linux – 从nginx缓慢下载大型静态文件

前端之家收集整理的这篇文章主要介绍了linux – 从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

更新:

调试日志完全为空,只有当我手动取消下载时才会出现以下错误

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"

卷曲输出

% 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

解决方法

通过Google为这里的任何人提供答案:

Sendfile是阻塞的,并且不允许Nginx设置前瞻,因此如果文件只读取一次,效率非常低.

Sendfile依赖于文件系统缓存等,并且从未为这样的大文件做过.

你想要的是禁用大文件的sendfile,并使用directio(最好使用线程,因此它是非阻塞的).
仍然可以使用sendfile读取16MB以下的任何文件.

aio threads;
directio 16M;
output_buffers 2 1M;

sendfile on;
sendfile_max_chunk 512k;

通过使用directio,您可以直接从磁盘读取,在途中跳过许多步骤.

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

原文链接:https://www.f2er.com/linux/398332.html

猜你在找的Linux相关文章