nginx 响应时间太久

2022-08-04 15:47:37 +08:00
 vegetableChick

在用 nginx + uwsgi + Django 来启动服务的过程中, 发现uwsgi的响应时间很快, 但是nginx 的响应时间过长了, 然后浏览器得到 504 Gateway Time-out异常

以下是一些相关的配置:

uwsgi config

[uwsgi]
pythonpath=/path/to/pythonpath
chdir=/path/to/chdir
env=DJANGO_SETTINGS_MODULE=conf.settings
module=moudle.wsgi
master=True
pidfile=logs/pidfile.pid
vacuum=True
max-requests=1000
enable-threads=true
processes = 4
threads=8
listen=1024
daemonize=logs/wsgi.log
http=0.0.0.0:10205
buffer-size=32000
socket-timeout=1500
harakiri=1500
http-timeout=1500
nginx config

nginx.conf

worker_processes  12;

events {
    use epoll;
    worker_connections  65535;
}


http {
    include       mime.types;
    include       log_format.conf;
    include       upstream.conf;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  1800;
    server_tokens off;

    client_max_body_size 100m;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 5;
    gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    include "site-enabled/*.conf";
    proxy_buffering on;
    proxy_buffer_size 4k;
    proxy_buffers 8 1M;
    proxy_busy_buffers_size 2M;
    proxy_max_temp_file_size 0;
}

log_format.conf

log_format upstream '$remote_addr - $host [$time_local] "$request" '
                    '$status $body_bytes_sent $request_time $upstream_response_time '
                    '"$http_user_agent" "$http_x_forwarded_for" ';

upstream.conf


upstream my_service {
        server host:16020  weight=50;
        server host:16020  weight=50;
        keepalive 100;
}

site-enabled/my_service.conf

server {
    listen 7020;
    server_name  my-service.xxx.cn;
    client_max_body_size 100M;
    access_log  logs/my_service_access.log  upstream;
    root /path/to/my_service/dist;

    location ^~ /api/base_api {
        proxy_redirect off;
        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_read_timeout 90;
        proxy_pass http://my_service;
        uwsgi_buffering  on;
        uwsgi_buffers    8 8k;
        uwsgi_buffer_size  8k;
    }

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

调用 api 后 uwsgi响应很快

[pid: 8841|app: 0|req: 4390/12492] xxx.xxx.xxx.xxx () {44 vars in 1103 bytes} [Thu Aug  4 14:13:23 2022] GET /api/account_opening_review/aor?page_size=1000 => generated 1668926 bytes in 499 msecs (HTTP/1.0 200) 4 headers in 119 bytes (1 switches on core 3)

但是 nginx响应很耗时

xxx.xxx.xxx.xxx - host [04/Aug/2022:14:25:05 +0800] "GET /api/account_opening_review/aor?page_size=1000 HTTP/1.1" 499 0 60.000 60.000 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "xxx.xxx.xxx.xxx"

如何加速nginx的响应速度呢? 加上响应的缓存好像也不生效, 或者是我写的有问题?

请大家帮忙看一下, 谢谢了!

2839 次点击
所在节点    Python
24 条回复
dem0ns
2022-08-04 15:50:25 +08:00
504 Gateway Time-out 不是 nginx 响应太久,而是 nginx 等后端响应得太久
vegetableChick
2022-08-04 15:52:15 +08:00
@dem0ns 看`uwsgi`不是已经返回响应了吗, `generated 1668926 bytes in 499 msecs`.
dem0ns
2022-08-04 15:54:22 +08:00
有很多可能

upstream my_service {
server host:16020 weight=50;
server host:16020 weight=50;
keepalive 100;
}

比如你的 host 其中一个卡住了


或者 uwsgi 不支持并发访问等等


但基本排除 nginx 自己的问题
vegetableChick
2022-08-04 16:05:07 +08:00
@dem0ns 和`uwsgi`返回数据的大小有关吗, 我这个示例响应数据有几 M 吧
vegetableChick
2022-08-04 16:05:59 +08:00
@dem0ns 请问, 有什么好的方法去定位一下您说的原因吗
dem0ns
2022-08-04 16:11:38 +08:00
http=0.0.0.0:10205



upstream my_service {
server host:16020 weight=50;
server host:16020 weight=50;
keepalive 100;
}



我看见你两个端口不一致,是不是这个问题?
gengchun
2022-08-04 16:17:03 +08:00
我觉得又见到了一个 proxy_pass 调的公网地址,然后公网 1M 带宽的。

uwsgi 肯定也没有开 gzip 。
vegetableChick
2022-08-04 16:20:31 +08:00
@dem0ns 抱歉这个端口是一致的 我写错了
vegetableChick
2022-08-04 16:35:33 +08:00
@gengchun 不好意思, 您能展开讲一下么, 不甚感激
encro
2022-08-04 16:45:07 +08:00
@vegetableChick

upstream my_service {
server host:16020 weight=50;
server host:16020 weight=50;
keepalive 100;
}

host 要换成内网 IP 。
如果没问题,那么就是带宽限制了。
encro
2022-08-04 16:47:23 +08:00
nginx 60 秒超时了,就没连上。
gengchun
2022-08-04 16:49:19 +08:00
@encro 如果是全部超时,那就是防火墙都没有开。
bigpigB
2022-08-04 16:59:47 +08:00
@gengchun 点赞同,其实 uwsgi 已经返回到 nginx 端了,这个时间是 nginx 和 client 客户端 data send 的过程(nginx 默认 send proxy timeout 60s),出现问题是在 client 跟 nginx 之间数据传输?
ggvm
2022-08-04 17:00:46 +08:00
把 nginx 去掉,直接看 cgi 需要多少执行时间。

再你 py 代码上加上一些时间统计的东西看看。
vegetableChick
2022-08-04 17:01:56 +08:00
@gengchun
@encro 返回数据量小是没有问题的
vegetableChick
2022-08-04 17:11:35 +08:00
@bigpigB 有什么办法可以加快这个传输吗?
wonderfulcxm
2022-08-04 17:17:42 +08:00
504 是 upstream 的问题,你去 nginx 那台 Server curl 请求下 proxy 的那个服务
encro
2022-08-04 17:44:05 +08:00
@vegetableChick

那就是你带宽被限制了。
vegetableChick
2022-08-05 10:17:10 +08:00
@gengchun 咨询了运维同学 带宽是 50M
yc8332
2022-08-05 11:05:27 +08:00
看你 nginx 的日志是 499 ,这个是客户端和 nginx 断开了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/870670

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX