扩展和负载测试 Django Nginx 和 Gunicorn:502 服务器错误 + 资源暂时不可用 gunicorn.sock

扩展和负载测试 Django Nginx 和 Gunicorn:502 服务器错误 + 资源暂时不可用 gunicorn.sock

我正在使用 locust 进行负载测试,看看我的服务器是否能处理 1500 个用户。

我正在使用:Django、Nginx、Gunicorn、Postgresql 我的 droplet:24vCPU、128GB RAM、25GB SSD

当用户数达到约 1100 时,我开始在 locust 中收到以下错误:

GET / HTTPError('502 Server Error: Bad Gateway for url: myurl.here ')
GET /aboutpage/     HTTPError('502 Server Error: Bad Gateway for url: myurl.here ')

在我的 nginx error.log 中我收到以下错误:

2020/01/26 23:14:17 [error] 30465#30465: *167765 connect() to unix:/var/www/file/to/sock/gunicorn failed (11: Resource temporarily unavailable) while connecting to upstream, client: 8x.8x.1xx.3x, server: mysite.here, request: "GET // HTTP/1.1", upstream: "http://unix:/var/www/file/to/sock/gunicorn://", host: "mysite.here"

由于某种原因,它告诉我我的资源暂时不可用。

这是我的 nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 2048;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

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

        # Virtual Host Configs
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

这是我的服务器块配置:


upstream mysite-production {
    server unix:/var/www/path/to/sock/gunicorn;
}
server {
    listen [::]:80;
    listen 80;
    server_name mysite.here;

    # set client body size to 100M #
    client_max_body_size 100M;

    location / {
      include proxy_params;
      proxy_pass http://unix:/var/www/path/to/sock/gunicorn;
      auth_basic "Restricted Content";
      auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location /static/ {
        root /var/www/site/production/;
        expires 30d;
        add_header Vary Accept-Encoding;
        access_log off;
        gzip on;
        gzip_comp_level 6;
        gzip_vary on;
        gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;
    }

    location /media/ {
        root /var/www/site/production/;
        expires 30d;
        add_header Vary Accept-Encoding;
        access_log off;
    }



}


这是我的 gunicorn 服务文件:

[Unit]
Description=mysite production daemon
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/path/to/site/src
ExecStart=/var/www/path/to/venv/bin/gunicorn  --workers=49 --bind unix:/var/www/path/to/sock/gunicorn --log-level DEBUG --log-file '/var/www/path/to/log/gunicorn.log' mysite.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

什么原因导致了这个问题?我们预计同时在线的用户将达到 1500-2000 人。

感谢您抽出时间!我期待您的回答!

答案1

在负载测试时,您总会遇到限制和瓶颈。

在你的情况下你需要增加gunicorn 配置backlog默认值变为2048您的服务器可以处理的值。

相关内容