nginx 响应时间太久 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
vegetableChick
V2EX    Python

nginx 响应时间太久

  •  
  •   vegetableChick 2022-08-04 15:47:37 +08:00 3599 次点击
    这是一个创建于 1164 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在用 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的响应速度呢? 加上响应的缓存好像也不生效, 或者是我写的有问题?

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

    24 条回复    2022-08-11 11:12:27 +08:00
    dem0ns     1
    dem0ns  
       2022-08-04 15:50:25 +08:00
    504 Gateway Time-out 不是 nginx 响应太久,而是 nginx 等后端响应得太久
    vegetableChick
        2
    vegetableChick  
    OP
       2022-08-04 15:52:15 +08:00
    @dem0ns 看`uwsgi`不是已经返回响应了吗, `generated 1668926 bytes in 499 msecs`.
    dem0ns
        3
    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
        4
    vegetableChick  
    OP
       2022-08-04 16:05:07 +08:00
    @dem0ns 和`uwsgi`返回数据的大小有关吗, 我这个示例响应数据有几 M 吧
    vegetableChick
        5
    vegetableChick  
    OP
       2022-08-04 16:05:59 +08:00
    @dem0ns 请问, 有什么好的方法去定位一下您说的原因吗
    dem0ns
        6
    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
        7
    gengchun  
       2022-08-04 16:17:03 +08:00
    我觉得又见到了一个 proxy_pass 调的公网地址,然后公网 1M 带宽的。

    uwsgi 肯定也没有开 gzip 。
    vegetableChick
        8
    vegetableChick  
    OP
       2022-08-04 16:20:31 +08:00
    @dem0ns 抱歉这个端口是一致的 我写错了
    vegetableChick
        9
    vegetableChick  
    OP
       2022-08-04 16:35:33 +08:00
    @gengchun 不好意思, 您能展开讲一下么, 不甚感激
    encro
        10
    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
        11
    encro  
       2022-08-04 16:47:23 +08:00
    nginx 60 秒超时了,就没连上。
    gengchun
        12
    gengchun  
       2022-08-04 16:49:19 +08:00
    @encro 如果是全部超时,那就是防火墙都没有开。
    bigpigB
        13
    bigpigB  
       2022-08-04 16:59:47 +08:00
    @gengchun 点赞同,其实 uwsgi 已经返回到 nginx 端了,这个时间是 nginx 和 client 客户端 data send 的过程(nginx 默认 send proxy timeout 60s),出现问题是在 client 跟 nginx 之间数据传输?
    ggvm
        14
    ggvm  
       2022-08-04 17:00:46 +08:00
    把 nginx 去掉,直接看 cgi 需要多少执行时间。

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

    那就是你带宽被限制了。
    vegetableChick
        19
    vegetableChick  
    OP
       2022-08-05 10:17:10 +08:00
    @gengchun 咨询了运维同学 带宽是 50M
    yc8332
        20
    yc8332  
       2022-08-05 11:05:27 +08:00
    看你 nginx 的日志是 499 ,这个是客户端和 nginx 断开了。
    yc8332
        21
    yc8332  
       2022-08-05 11:09:08 +08:00
    你可以试试单个连接会不会有问题,如果没有问题,那就是你的 python 那边有问题。比如不支持并发之类的
    buffzty
        22
    buffzty  
       2022-08-05 11:35:38 +08:00
    最简单有效的方式 tcpdump 抓包 分析
    gengchun
        23
    gengchun  
       2022-08-05 13:59:18 +08:00
    @vegetableChick 都有运维了让运维解决。
    hope4tomorrow
        24
    hope4tomorrow  
       2022-08-11 11:12:27 +08:00
    先简化配置,然后 reload ,看能不能复现,通过缩小配置范围,缩小问题出现的范围
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2193 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 00:45 PVG 08:45 LAX 17:45 JFK 20:45
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86