【发布时间】:2016-08-02 23:42:55
【问题描述】:
我有一个运行良好的 Nginx 配置后面的 Node.js 应用服务器。我预计负载会增加一些,并认为我可以通过设置另一个 Nginx 来为 Node.js 应用服务器上的静态文件提供服务。所以,基本上我已经在 Nginx 和 Node.js 前面设置了 Nginx 反向代理。
当我重新加载 Nginx 并让它开始为路由 /publicfile/ 上的请求 (NginxNginx) 提供服务时,我注意到速度显着下降。花费了NginxNode.js 大约 3 秒的东西没有花费 NginxNginx ~15 秒!
我是 Nginx 的新手,一天中大部分时间都花在这上面,最后决定发帖寻求社区帮助。谢谢!
面向 Nginx 的 web nginx.conf:
http {
# Main settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_header_timeout 1m;
client_body_timeout 1m;
client_header_buffer_size 2k;
client_body_buffer_size 256k;
client_max_body_size 256m;
large_client_header_buffers 4 8k;
send_timeout 30;
keepalive_timeout 60 60;
reset_timedout_connection on;
server_tokens off;
server_name_in_redirect off;
server_names_hash_max_size 512;
server_names_hash_bucket_size 512;
# Log format
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format bytes '$body_bytes_sent';
access_log /var/log/nginx/access.log main;
# Mime settings
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Compression
gzip on;
gzip_comp_level 9;
gzip_min_length 512;
gzip_buffers 8 64k;
gzip_types text/plain text/css text/javascript
application/x-javascript application/javascript;
gzip_proxied any;
# Proxy settings
#proxy_redirect of;
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_pass_header Set-Cookie;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
real_ip_header CF-Connecting-IP;
# SSL PCI Compliance
# - removed for brevity
# Error pages
# - removed for brevity
# Cache
proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=512m;
proxy_cache_key "$host$request_uri $cookie_user";
proxy_temp_path /var/cache/nginx/temp;
proxy_ignore_headers Expires Cache-Control;
proxy_cache_use_stale error timeout invalid_header http_502;
proxy_cache_valid any 3d;
proxy_http_version 1.1; # recommended with keepalive connections
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
map $http_cookie $no_cache {
default 0;
~SESS 1;
~wordpress_logged_in 1;
}
upstream backend {
# my 'backend' server IP address (local network)
server xx.xxx.xxx.xx:80;
}
# Wildcard include
include /etc/nginx/conf.d/*.conf;
}
面向网络的 Nginx Server 块将静态文件转发到它后面的 Nginx(在另一个盒子上):
server {
listen 80 default;
access_log /var/log/nginx/nginx.log main;
# pass static assets on to the app server nginx on port 80
location ~* (/min/|/audio/|/fonts/|/images/|/js/|/styles/|/templates/|/test/|/publicfile/) {
proxy_pass http://backend;
}
}
最后是“后端”服务器:
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
sendfile_max_chunk 32;
# server_tokens off;
# server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
root /home/admin/app/.tmp/public;
listen 80 default;
access_log /var/log/nginx/app-static-assets.log;
location /publicfile {
alias /home/admin/APP-UPLOADS;
}
}
}
【问题讨论】:
-
你的配置很好。不过也有一些考虑。您说后端服务器位于不同的盒子上。它们在同一个物理网络上吗?这可能解释了性能下降的原因,尤其是如果不是这样的话。看起来好像您在后端框中没有
gzip压缩。现在,从前端它看起来好像正在被压缩(因为它是),但是文件在 2 个盒子之间以未压缩的形式发送。此外,请尝试将压缩级别降低到 6 左右。之后它不会压缩太多,您将释放 CPU 周期。 -
最后,您需要研究
sendfile_max_chunk指令,并试验参数。查看here 的案例研究。关闭访问日志也将提高性能,但如果您要交付的不是 1000 个静态文件,则不会提高多少。 -
@KeenanLawrence 感谢您查看代码。它们在同一个物理网络上,并且我使用本地网络 IP 进行连接,因此它们不会在本地网络之外进行通信。
sendfile_max_chunk是对的,成功了!