Nginx + uWSGI + Django 太慢了
Nginx + uWSGI + Django too slow
我在 nginx 和 uwsgi 上安装了 django 运行。缓存的响应加载速度非常快,但在其他时候网站加载时间超过 30 秒。我无法诊断速度变慢的根本原因。以下是我可以提供的信息,以帮助缩小问题范围 -
GTMetrix - 我可以从瀑布报告中得出的结论是静态文件的等待时间与初始服务器响应时间太多。这是更详细的细分:
Link to the lighthouse parameters Waterfall report
nginx.conf - 这是 nginx 配置文件:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75;
types_hash_max_size 2048;
client_max_body_size 5M;
sendfile_max_chunk 512;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format upstream_time '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"'
'rt="$request_time" uct="$upstream_connect_time"
uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log upstream_time;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable msie6;
# And all the gzip mime types here
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive 60m use_temp_path off;
server {
location ~* \.(jpg|jpeg|png|gif|ico|css|js){
proxy_cache my_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503
http_504;
proxy_cache_lock on;
expires 365d;
proxy_pass http://example.net;
}
}
}
Nginx 项目配置 -
map $sent_http_content_type $expires{
default on;
text/html epoch;
text/css max;
appplication/javascript max;
~image/ max;
}
server{
listen 80;
server_name example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/mysite/project_dir/app_dir;
expires $expires;
}
location /images/ {
expires $expires;
root /home/mysite/project_dir/app_dir/static/images/;
}
location /media/ {
expires $expires;
root /home/mysite/project_dir/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/mysite.sock;
gzip_static on;
proxy_buffering off;
proxy_cache my_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503
http_504;
proxy_cache_lock on;
expires 365d;
proxy_set_header X-Real-IP $remote-addr;
proxy_set_header Host $http-host;
proxy_set_header Connection "";
}
listen 443 ssl http2;#Managed by certbot
#All the subsequent certbot settings not tampered with
}
Logs - 因此,当我使用上述配置登录 nginx 时,访问日志显示 upstream_response_time 只有当网站被缓存加载时才完美。当加载时间超过 30 秒时,upstream_response_time 包括除 response_time 之外的所有参数显示连字符 '-'。
更新:
- django-debug-toolbar- 资源使用:
资源
价值
用户CPU时间
964.000 毫秒
系统CPU时间
52.000 毫秒
总 CPU 时间
1016.000 毫秒
系统CPU时间
1019.185 毫秒
所有 SQL 查询花费的时间最少(10.78 毫秒)。记录器也显示 0 个错误。
如果有人能帮助我诊断这种放缓的根本原因,我将不胜感激。谢谢!
呸!所以我想出了解决方案。我使用了 - https://www.webpagetest.org 并得出初始连接时间非常长(~30 秒)的结论。当它发生时,很可能是 dns/firewall 问题。我的问题是基于 DNS 的。我有 2 个 ips 作为 A 记录添加到我的域中。一个是私人IP。所以浏览器实际上需要大约 30 秒来加载该 ip,当网站加载时,浏览器会缓存响应,因此后续响应时间很短。简单地删除私有 ip 对我有用。
我在 nginx 和 uwsgi 上安装了 django 运行。缓存的响应加载速度非常快,但在其他时候网站加载时间超过 30 秒。我无法诊断速度变慢的根本原因。以下是我可以提供的信息,以帮助缩小问题范围 -
GTMetrix - 我可以从瀑布报告中得出的结论是静态文件的等待时间与初始服务器响应时间太多。这是更详细的细分: Link to the lighthouse parameters Waterfall report
nginx.conf - 这是 nginx 配置文件:
user www-data; worker_processes 4; pid /run/nginx.pid; events { worker_connections 768; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 75; types_hash_max_size 2048; client_max_body_size 5M; sendfile_max_chunk 512; include /etc/nginx/mime.types; default_type application/octet-stream; log_format upstream_time '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"' 'rt="$request_time" uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"'; access_log /var/log/nginx/access.log upstream_time; error_log /var/log/nginx/error.log; gzip on; gzip_disable msie6; # And all the gzip mime types here include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive 60m use_temp_path off; server { location ~* \.(jpg|jpeg|png|gif|ico|css|js){ proxy_cache my_cache; proxy_cache_revalidate on; proxy_cache_min_uses 3; proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; proxy_cache_lock on; expires 365d; proxy_pass http://example.net; } } }
Nginx 项目配置 -
map $sent_http_content_type $expires{ default on; text/html epoch; text/css max; appplication/javascript max; ~image/ max; } server{ listen 80; server_name example.com; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/mysite/project_dir/app_dir; expires $expires; } location /images/ { expires $expires; root /home/mysite/project_dir/app_dir/static/images/; } location /media/ { expires $expires; root /home/mysite/project_dir/; } location / { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/mysite.sock; gzip_static on; proxy_buffering off; proxy_cache my_cache; proxy_cache_revalidate on; proxy_cache_min_uses 3; proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; proxy_cache_lock on; expires 365d; proxy_set_header X-Real-IP $remote-addr; proxy_set_header Host $http-host; proxy_set_header Connection ""; } listen 443 ssl http2;#Managed by certbot #All the subsequent certbot settings not tampered with }
Logs - 因此,当我使用上述配置登录 nginx 时,访问日志显示 upstream_response_time 只有当网站被缓存加载时才完美。当加载时间超过 30 秒时,upstream_response_time 包括除 response_time 之外的所有参数显示连字符 '-'。
更新:
- django-debug-toolbar- 资源使用:
资源 | 价值 |
---|---|
用户CPU时间 | 964.000 毫秒 |
系统CPU时间 | 52.000 毫秒 |
总 CPU 时间 | 1016.000 毫秒 |
系统CPU时间 | 1019.185 毫秒 |
所有 SQL 查询花费的时间最少(10.78 毫秒)。记录器也显示 0 个错误。
如果有人能帮助我诊断这种放缓的根本原因,我将不胜感激。谢谢!
呸!所以我想出了解决方案。我使用了 - https://www.webpagetest.org 并得出初始连接时间非常长(~30 秒)的结论。当它发生时,很可能是 dns/firewall 问题。我的问题是基于 DNS 的。我有 2 个 ips 作为 A 记录添加到我的域中。一个是私人IP。所以浏览器实际上需要大约 30 秒来加载该 ip,当网站加载时,浏览器会缓存响应,因此后续响应时间很短。简单地删除私有 ip 对我有用。