nginx-entrypoint.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/sh
  2. set -e
  3. CORES=$(nproc)
  4. echo "$CORES cores detected, starting $CORES aiohttp workers..."
  5. for i in $(seq 0 $((CORES-1))); do
  6. SOCKET="/run/aiohttp-$i.sock"
  7. echo "Starting worker on socket $SOCKET"
  8. python3 -O -m app.app --socket $SOCKET &
  9. done
  10. echo "Waiting for all workers to be ready..."
  11. for i in $(seq 0 $((CORES-1))); do
  12. SOCKET="/run/aiohttp-$i.sock"
  13. until [ -S "$SOCKET" ]; do
  14. echo "Waiting for socket $SOCKET..."
  15. sleep 0.2
  16. done
  17. chown root:www-data "$SOCKET"
  18. chmod 660 "$SOCKET"
  19. done
  20. cat > /aiohttp/nginx.conf <<EOF
  21. user www-data;
  22. worker_processes auto;
  23. events {
  24. worker_connections 65535;
  25. }
  26. http {
  27. keepalive_requests 10000000;
  28. upstream aiohttp {
  29. least_conn;
  30. EOF
  31. for i in $(seq 0 $((CORES-1))); do
  32. echo " server unix:/run/aiohttp-$i.sock fail_timeout=0;" >> /aiohttp/nginx.conf
  33. done
  34. cat >> /aiohttp/nginx.conf <<EOF
  35. keepalive 32;
  36. }
  37. server {
  38. listen 8080 reuseport;
  39. access_log off;
  40. error_log stderr error;
  41. location / {
  42. proxy_pass http://aiohttp;
  43. proxy_http_version 1.1;
  44. proxy_set_header Connection "";
  45. proxy_redirect off;
  46. proxy_buffering off;
  47. }
  48. }
  49. }
  50. EOF
  51. echo "Starting Nginx..."
  52. nginx -c /aiohttp/nginx.conf -g "daemon off;"