在用 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
confignginx.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
的响应速度呢? 加上响应的缓存好像也不生效, 或者是我写的有问题?
请大家帮忙看一下, 谢谢了!
1 dem0ns 2022-08-04 15:50:25 +08:00 504 Gateway Time-out 不是 nginx 响应太久,而是 nginx 等后端响应得太久 |
![]() | 2 vegetableChick OP @dem0ns 看`uwsgi`不是已经返回响应了吗, `generated 1668926 bytes in 499 msecs`. |
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 自己的问题 |
![]() | 4 vegetableChick OP @dem0ns 和`uwsgi`返回数据的大小有关吗, 我这个示例响应数据有几 M 吧 |
![]() | 5 vegetableChick OP @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; } 我看见你两个端口不一致,是不是这个问题? |
7 gengchun 2022-08-04 16:17:03 +08:00 我觉得又见到了一个 proxy_pass 调的公网地址,然后公网 1M 带宽的。 uwsgi 肯定也没有开 gzip 。 |
![]() | 8 vegetableChick OP @dem0ns 抱歉这个端口是一致的 我写错了 |
![]() | 9 vegetableChick OP @gengchun 不好意思, 您能展开讲一下么, 不甚感激 |
![]() | 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 。 如果没问题,那么就是带宽限制了。 |
![]() | 11 encro 2022-08-04 16:47:23 +08:00 nginx 60 秒超时了,就没连上。 |
13 bigpigB 2022-08-04 16:59:47 +08:00 @gengchun 点赞同,其实 uwsgi 已经返回到 nginx 端了,这个时间是 nginx 和 client 客户端 data send 的过程(nginx 默认 send proxy timeout 60s),出现问题是在 client 跟 nginx 之间数据传输? |
14 ggvm 2022-08-04 17:00:46 +08:00 把 nginx 去掉,直接看 cgi 需要多少执行时间。 再你 py 代码上加上一些时间统计的东西看看。 |
![]() | 15 vegetableChick OP |
![]() | 16 vegetableChick OP @bigpigB 有什么办法可以加快这个传输吗? |
![]() | 17 wonderfulcxm 2022-08-04 17:17:42 +08:00 504 是 upstream 的问题,你去 nginx 那台 Server curl 请求下 proxy 的那个服务 |
![]() | 18 encro 2022-08-04 17:44:05 +08:00 |
![]() | 19 vegetableChick OP @gengchun 咨询了运维同学 带宽是 50M |
20 yc8332 2022-08-05 11:05:27 +08:00 看你 nginx 的日志是 499 ,这个是客户端和 nginx 断开了。 |
21 yc8332 2022-08-05 11:09:08 +08:00 你可以试试单个连接会不会有问题,如果没有问题,那就是你的 python 那边有问题。比如不支持并发之类的 |
22 buffzty 2022-08-05 11:35:38 +08:00 最简单有效的方式 tcpdump 抓包 分析 |
23 gengchun 2022-08-05 13:59:18 +08:00 @vegetableChick 都有运维了让运维解决。 |
![]() | 24 hope4tomorrow 2022-08-11 11:12:27 +08:00 先简化配置,然后 reload ,看能不能复现,通过缩小配置范围,缩小问题出现的范围 |