在 Azure 应用服务上使用 nginx+gunicorn 应用程序获取超时错误

在 Azure 应用服务上使用 nginx+gunicorn 应用程序获取超时错误

伙计们,我需要一些有关 NGINX 配置的帮助。目前,Django 应用程序托管在 Azure App 服务上,直接访问 Gunicorn 可以正常工作,但是当我访问 NGINX 时,我开始收到错误,如下所示:

我尝试增加超时时间,但错误仍然零星地出现在不同的端点上,而且当我不使用 NGINX 而只是使用 gunicorn 时端点工作正常,所以我猜这与 NGINX 设置有关。


    2022-01-26T10:22:03.479463450Z nginx      | 2022/01/26 10:22:03 [info] 29#29: *2245 epoll_wait() reported that client prematurely closed connection, so upstream connection is closed too while sending request to upstream, client: 169.254.130.1, server: xxxxx.com, request: "GET /api/v1/office-hours/ HTTP/1.1", upstream: "http://127.0.0.1:8000/api/v1/office-hours/", host: "xxxxx.com", referrer: "https://xxxx.vercel.app/"
    2022-01-26T10:22:03.514362267Z nginx      | 169.254.130.1 - - [26/Jan/2022:10:22:03 +0000] "GET /api/v1/office-hours/ HTTP/1.1" 499 0 "https://xxxx.vercel.app/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
     
    2022-01-26T10:23:03.513182059Z nginx      | 2022/01/26 10:23:02 [info] 29#29: *2247 epoll_wait() reported that client prematurely closed connection, so upstream connection is closed too while sending request to upstream, client: 169.254.130.1, server: xxxxx.com, request: "GET /api/v1/office-hours/ HTTP/1.1", upstream: "http://127.0.0.1:8000/api/v1/office-hours/", host: "xxxxx.com", referrer: "https://xxxx.vercel.app/"
    2022-01-26T10:23:03.513238060Z nginx      | 169.254.130.1 - - [26/Jan/2022:10:23:02 +0000] "GET /api/v1/office-hours/ HTTP/1.1" 499 0 "https://xxxx.vercel.app/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" 

web        | [2022-01-25 17:21:07 +0000] [15] [CRITICAL] WORKER TIMEOUT (pid:32)
2022-01-25T17:21:07.490138324Z web        | 2022-01-25 18:21:07,487 [32mINFO   [31mglogging   [33mWorker exiting (pid: 32)   [34mgunicorn.error.info:264[0m
2022-01-25T17:21:07.490220225Z nginx      | 2022/01/25 17:21:07 [error] 25#25: *930 upstream prematurely closed connection while reading response header from upstream, client: 169.254.130.1, server: xxxx.com, request: "GET /api/v1/cms/content/ HTTP/1.1", upstream: "http://127.0.0.1:8000/api/v1/cms/content/", host: "xxxx.com"
2022-01-25T17:21:07.490875232Z nginx      | 169.254.130.1 - - [25/Jan/2022:17:21:07 +0000] "GET /api/v1/cms/content/ HTTP/1.1" 502 158 "-" "axios/0.18.1"
2022-01-25T17:21:07.490892833Z nginx      | 2022/01/25 17:21:07 [info] 25#25: *930 client 169.254.130.1 closed keepalive connection
2022-01-25T17:21:08.673367528Z web        | [2022-01-25 17:21:08 +0000] [15] [WARNING] Worker with pid 32 was terminated due to signal 9
2022-01-25T17:21:08.679932505Z web        | [2022-01-25 17:21:08 +0000] [43] [INFO] Booting worker with pid: 43

这是我的 nginx.conf

#user  nginx;
worker_processes  2; # Set to number of CPU cores, 2 cores under Azure plan P1v3

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

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

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

这是我的 default.conf

server {
listen 80 default_server;

error_log /dev/stdout info;
access_log /dev/stdout;

client_max_body_size 100M;


location /static {
    root /var/app/ui/build;
}

location /site-static {
    root /var;
}

location /media {
    root /var;
}

location / {
    root /var/app/ui/build; # try react build directory first, if file doesn't exist, route requests to django app
    try_files $uri $uri/index.html $uri.html @app;
}

location @app {
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto "https"; # assumes https already terminated by the load balancer in front of us

    proxy_pass          http://127.0.0.1:8000;
    proxy_read_timeout  300;
    proxy_buffering    off;
}

}

答案1

您需要调试您的应用程序,为什么它需要这么长时间才能响应。

nginx 发送上游请求和 nginx 放弃​​等待响应之间有一分钟的延迟。

nginx 可以通过 TCP 连接到上游应用程序,它可以发送 HTTP 请求。但是,在 nginx 超时之前,应用程序不会发送响应。

相关内容