Nginx 慢速静态文件服务(比节点慢?)

Nginx slow static file serving (slower than node?)

我有一个 Node.js 应用服务器位于运行良好的 Nginx 配置后面。我预计会增加一些负载,并认为我可以通过设置另一个 Nginx 来为 Node.js 应用程序服务器上的静态文件提供服务。所以,基本上我在 Nginx & Node.js.

前面设置了 Nginx 反向代理

当我重新加载 Nginx 并让它开始处理路由 /publicfile/ 上的请求 (Nginx<->Nginx) 时,我注意到速度明显下降。花费了 Nginx<->Node.js 大约 3 秒的东西没有花费 Nginx<->Nginx ~15 秒!

我是 Nginx 的新手,一天的大部分时间都花在这上面,最后决定 post 寻求一些社区帮助。谢谢!

面向Nginx的网络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 的 Web 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;
  }
}

最后 "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;
  }
 } 
} 

@keenanLawrence 在上面的评论中提到,sendfile_max_chunk 指令。

sendfile_max_chunk 设置为 512k 后,我发现从 Nginx 传送静态文件(从磁盘)的速度有了显着提高。

我从8k32k128k、最后512k开始试验,区别好像是每个服务器的最佳配置 chunk size 取决于传送的内容、可用线程和服务器请求负载。

当我将 worker_processes auto; 更改为 worker_processes 2; 时,我还注意到另一个显着的性能提升,即从在每个 cpu 上使用 worker_process 到仅使用 2.在我的例子中,这更有效,因为我在同一台机器上也有 Node.js 应用程序服务器 运行,并且它们也在 cpu 上执行操作。