在用 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
的响应速度呢? 加上响应的缓存好像也不生效, 或者是我写的有问题?
请大家帮忙看一下, 谢谢了!
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.