Nginx 错误 - 110:连接到上游时连接超时(django-python)

Nginx 错误 - 110:连接到上游时连接超时(django-python)

错误是

2021/08/18 8:57:28 [error] 19915#19915: *36133 upstream timed out (110: Connection timed out) while connecting to upstream, client: 10.11.12.1(proxy-ip), server: example.com, request: "GET /static/css/bootstrap.min.css HTTP/1.1", upstream: "http://127.0.0.1:8000/static/css/bootstrap.min.css", host: "www.example.com"

错误简要描述

网站一直运行良好,但有时会出现上述错误,我认为这是由于网站流量过大造成的,当网站瘫痪时,重新启动 nginx 和 Supervisor 后网站也不会立即启动

有时网站需要 5 到 6 个小时才能打开

服务器 - Ubuntu-18.04 LTS

网络服务器配置如下

我有一个代理服务器和应用程序服务器

  1. 代理服务器-nginx

  2. 应用服务器 - 运行 nginx 和(主管 - django 应用程序)

  3. 代理服务器-nginx-config

     server {
    
         listen 443 ssl http2;
         ssl_certificate /etc/nginx/ssl/bundle.crt;
         ssl_certificate_key /etc/nginx/ssl/start.example.com.key;
         server_name example.com www.example.com ;
         location = /basic_status {
         stub_status;
         access_log   off;
         allow 1.2.3.4;
         deny all;
         }
         location /{
    
         proxy_connect_timeout       300;
         proxy_send_timeout          300;
         proxy_read_timeout          300;
         send_timeout                300;
         proxy_pass http://10.11.12.2;  #proxy to application server
         proxy_http_version 1.1;
         proxy_set_header Connection "";
         add_header Access-Control-Allow-Origin .example.com;
         add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
         add_header X-XSS-Protection "1; mode=block";
         add_header "Pragma" "no-cache"; 
         proxy_set_header Host $http_host;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Scheme $scheme;
    
         #add_header Content-Security-Policy "default-src 'unsafe-inline' 'self'; script-src 'unsafe-inline' 'self' ";
         error_page 404 /404.html;
         location = /404.html {
         root /var/www/error;
         internal;
         }
         error_page 500 502 503 504 /500.html;
         location = /500.html {
         root /usr/share/nginx/html;
         internal;
         }
    
     }
    
  4. 应用服务器-nginx

    server {
             listen 80 default_server;
             listen [::]:80 default_server;
             server_name example.com www.example.com;
    
             location /{
    
                 proxy_pass http://127.0.0.1:8000;
                 proxy_read_timeout 180;
    
             }                                                                                                                    
      }
    
  5. 应用服务器 - 主管

     [program:portal]
     command =/root/portal_env/bin/gunicorn  portal.wsgi:application -b 0.0.0.0:8000 --timeout 180 --workers=3 ;
     user = root                                                ; User to run as
     directory = /root/portal_env/portal
     stdout_logfile = /root/portal_env/logs/portal.log ; Where to write log messages
     redirect_stderr = true                                       ; Save stderr in the same log
     autostart = true
     autorestart = true
     environment = LANG = en_US.UTF-8,LC_ALL = en_US.UTF-8              ; Set UTF-8 as default encoding
    

答案1

首先,我会对您的设置提供一些反馈。

当你构建 Python 应用程序时,不建议使用 gunicorn 作为服务器主机,它是为开发而构建的,你可以使用 uWSGI 作为你的 Python 应用程序。

接下来,当您在 NGINX 中使用代理时,我建议您禁用代理的缓冲

proxy_buffering                 off;

当你禁用缓冲时,网站的加载速度会稍微慢一些,因为客户端需要将完整的包从服务器发送到客户端。我的到期者告诉我,当我们进行 Web 开发时,情况就没那么重要了,如果你想在 NGINX Proxy、Docker Cluster 或类似的东西之类的负载均衡器后面的多服务器上运行你的应用程序,它可以长期保护你。

根据我的反馈,听起来你的代码中有些东西进入了循环并且永不停止,或者你有一个很长的 SQL 连接/查询需要超过 3 分钟才能执行。

我在这里建议您在数据库上启用慢速日志,这样您就可以获得所有查询的日志,这些查询需要超过 0.5 秒,这可能是因为您的数据库中缺少索引,从而影响网站的性能。

普通的 Python 并不需要 3 分钟来执行脚本/代码区域,所以在这种情况下它听起来像是外部服务,例如数据库、api 或某些东西。

当您为数据库启用了慢速日志时,第二种情况是为您的脚本创建一个带有“开始时间”+“结束时间”的简单日志,并将其与访问该区域的方法/页面/url 一起转储到文件中,以便您轻松打开它,稍后它会在您所说 5-6 小时内运行或更长时间以捕获您的错误。

我认为这是最好的方法,第一个难题就是找出为什么你的代码需要 3 分钟才能执行。

相关内容