慢速从nginx下载大的静态文件

我在vmware-esxi虚拟化中使用了debian 7 x64。

每个客户端的最大下载量是1mb / s,nginx不能超过50mbps,我的问题是什么可能导致如此缓慢的传输?

服务器

**Settings for eth1: Supported ports: [ TP ] Supported link modes: 1000baseT/Full 10000baseT/Full** root@www:~# iostat Linux 3.2.0-4-amd64 (www) 09.02.2015 _x86_64_ (4 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 1,75 0,00 0,76 0,64 0,00 96,84 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 173,93 1736,11 219,06 354600 44744 root@www:~# free -m total used free shared buffers cached Mem: 12048 1047 11000 0 106 442 -/+ buffers/cache: 498 11549 Swap: 713 0 713 

nginx.cof

 user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 3072; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 5; types_hash_max_size 2048; server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; ## # nginx-naxsi config ## # Uncomment it if you installed nginx-naxsi ## #include /etc/nginx/naxsi_core.rules; ## Start: Size Limits & Buffer Overflows ## client_body_buffer_size 1k; client_header_buffer_size 1k; client_max_body_size 4M; large_client_header_buffers 2 1k; ## END: Size Limits & Buffer Overflows ## ## Start: Timeouts ## client_body_timeout 10; client_header_timeout 10; send_timeout 10; ## End: Timeouts ## ## END: Size Limits & Buffer Overflof ## # nginx-passenger config ## # Uncomment it if you installed nginx-passenger ## #passenger_root /usr; #passenger_ruby /usr/bin/ruby; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

/etc/sysctl.conf中

 # Increase system IP port limits to allow for more connections net.ipv4.ip_local_port_range = 2000 65000 net.ipv4.tcp_window_scaling = 1 # number of packets to keep in backlog before the kernel starts dropping them net.ipv4.tcp_max_syn_backlog = 3240000 # increase socket listen backlog net.core.somaxconn = 3240000 net.ipv4.tcp_max_tw_buckets = 1440000 # Increase TCP buffer sizes net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 

更新:

debugging日志是完全空的,只有当我手动取消下载时,我得到以下错误

 2015/02/09 20:05:32 [info] 4452#0: *2786 client prematurely closed connection while sending response to client, client: 83.11.xxx.xxx, server: xxx.com, request: "GET filename HTTP/1.1", host: "xxx.com" 

curl输出:

  % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1309M 100 1309M 0 0 374M 0 0:00:03 0:00:03 --:--:-- 382M 

您可能需要更改sendfile_max_chunk值,如文档所述:

 Syntax: sendfile_max_chunk size; Default: sendfile_max_chunk 0; Context: http, server, location 

设置为非零值时,限制可以在单个sendfile()调用中传输的数据量。 没有限制,一个快速连接可能完全占用工作进程。

如果您的大部分stream量是“大”静态文件,您可能还需要调整缓冲区大小。

通过谷歌的任何人的答案:

发送文件被阻塞,并且不能使nginx设置超前,因此如果一个文件只被读取一次,效率非常低。

发送文件依赖于文件系统caching等',从来没有为这样的大文件。

你想要的是禁用大文件的sendfile,并使用directio(最好与线程,所以它是非阻塞),而不是。 任何16MB以下的文件将仍然使用sendfile读取。

 aio threads; directio 16M; output_buffers 2 1M; sendfile on; sendfile_max_chunk 512k; 

通过使用directio,您可以直接从磁盘读取数据,而不需要执行任何步骤。

ps请注意,要使用aio线程,您需要使用线程支持来编译nginx https://www.nginx.com/blog/thread-pools-boost-performance-9x/

您是否尝试过调整MTU (最大传输单元) – 可以在单个networking事务中传输的最大networking层协议数据单元的大小? 在我们的例子中,将其从1500个字节切换到4000个字节,大大提高了下载性能。 支持的MTU根据IP传输而不同。 尝试使用不同的值来评估您的用例中的大小是否合理。

您可以使用ifconfig检查现有的MTU大小,并使用以下命令在运行时更新它:

 ifconfig eth0 mtu 5000 

还请访问这个非常有用的文章如何通过networking传输大量的数据 ?