Большие файлы и 502 с помощью nginx, uwsgi и ELB

У меня возникают трудности при попытке отладить, почему я получаю 502s при загрузке больших файлов. Я продолжаю получать ошибки, что keepalive умирает, например

Когда я проверяю журналы nginx:

2021/11/17 19:17:04 [info] 33#33: *82 client 10.0.10.148 closed keepalive connection
2021/11/17 19:15:45 [info] 32#32: *104 client 10.0.20.96 closed keepalive connection

Когда я проверяю журналы uwsgi/app, я не вижу ничего особенного. Иногда я получаю ошибки ОС, такие как Wed Nov 17 17:28:37 2021 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request, но я не думаю, что они связаны.

Я также получаю 9 ошибок рабочего сигнала, но я думаю, что некоторые могут быть ошибками памяти DAMN ! worker 3 (pid: 19) died, killed by signal 9 :( trying respawn ...

Если это проблемы с памятью на рабочих uswgi, как мне увеличить память на процесс?

(Я не думаю, что это проблема с django, так как у меня нет проблем с загрузкой больших файлов через сервер разработки django локально)

nginx.conf:

http {
    access_log /dev/stdout;

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $upstream_addr '
        '"$http_referer" "$http_user_agent"';

    access_log      /var/log/nginx/access.log main;

    client_max_body_size 900M;
    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;

    client_header_timeout 1200;
    client_body_timeout 1200;

    server {
        listen  80 default_server;
        listen  [::]:80 default_server;
        server_name nginx localhost 0.0.0.0 127.0.0.1 18.144.134.122;
        charset utf-80;

        root /var/www;
        index index.html;

        gzip on;
        gzip_static on;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_proxied  any;
        gzip_vary on;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;

        location /health {
            return 200 "healthy\n";
        }

        location /ws/ {
            proxy_pass http://127.0.0.1:8001; # daphne (ASGI) listening on port 8001
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        # api proxy to uwsgi/django
        location ~ ^/api {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;

            uwsgi_param   Host                  $host;
            uwsgi_param X-Real-IP $remote_addr;
            uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
            uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

            proxy_read_timeout 1200;
            proxy_send_timeout 1200;
            uwsgi_read_timeout 1200s;
            uwsgi_socket_keepalive on;
        }


        location / {
            try_files $uri /index.html;
        }
    }

uwsgi.ini

[uwsgi]
module=backend.wsgi

protocol=uwsgi
socket=0.0.0.0:8000

auto-procname = true
procname-prefix = "api "

strict = true
master = true
enable-threads = true
vacuum = true                        ; Delete sockets during shutdown
single-interpreter = true
die-on-term = true                   ; Shutdown when receiving SIGTERM (default is respawn)
need-app = true

log-4xx = true                              ; but log 4xx's anyway
log-5xx = true                              ; and 5xx's

http-timeout = 1200
socket-timeout = 1200

processes = 4                                              ; Maximum number of workers allowed
max-requests = 1000                                        ; Restart workers after this many requests
max-worker-lifetime = 600 ; seconds (5m)                   ; Restart workers after this many seconds
reload-on-rss = 256 ; mb  (allows for 4 workers for 1 GB)  ; Restart workers after this much resident memory
worker-reload-mercy = 60   ; mins                          ; How long to wait before forcefully killing workers

http-keepalive = true

настройка балансировщика нагрузки:

resource "aws_alb" "main" {
  for_each = var.instances
  name     = each.value.app_id

  internal        = local.internal
  subnets         = local.internal == true ? module.vpc.private_subnets : module.vpc.public_subnets
  security_groups = [aws_security_group.nsg_lb.id]
  tags            = local.tags

  idle_timeout = 660

  access_logs {
    enabled = true
    bucket  = aws_s3_bucket.lb_access_logs[each.key].bucket
  }
}
Вернуться на верх