将nginx配置为具有许多并发连接的转发代理

时间:2017-02-24 11:06:51

标签: linux nginx http-proxy

我无法处理多个同时活动的连接。对于1,000个活动连接,网络I / O达到大约1 Gb / s,但在将活动连接数增加一倍后,网络I / O会下降一半。在这两种情况下,每秒处理的请求都非常低,并且使用浏览器测试每个站点至少在30秒内加载。 CPU和RAM很难使用,它必须是配置问题。

问题是: 如何通过大量活动连接实现高网络I / O并每秒处理大量请求

当前的nginx配置:

user www-data;
worker_processes 4;
pid /run/nginx.pid;
worker_rlimit_nofile 40000;

events {
        use epoll;
        worker_connections 5000;
        multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 30;
        keepalive_requests 1000;
        client_body_timeout 10;
        send_timeout 2;
        types_hash_max_size 2048;
        server_tokens off;
        proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m    inactive=24h  max_size=1g;
        aio threads=default;
        default_type application/octet-stream;
        resolver 8.8.8.8;
        access_log off;
        error_log /var/log/nginx/error.log;
        gzip on;
        gzip_min_length 5000;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_disable "msie6";
        client_header_buffer_size 5k;
        open_file_cache max=10000 inactive=30s;
        open_file_cache_valid    60s;
        open_file_cache_min_uses 2;
        open_file_cache_errors   on;


        server {

            listen      80 default_server;
            server_name  proxy;
            server_tokens off;

            error_log  /var/log/nginx/proxy-error.log;

            error_page 502 /50x.html;

            location = /50x.html {
                    root /etc/nginx/custom_error_pages;
                    internal;
            }

            keepalive_timeout 10;
            proxy_set_header  Host  $host;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_set_header Accept-Encoding "";

            proxy_set_header X-REALIP   $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_buffering    off;
            proxy_intercept_errors off;
            proxy_max_temp_file_size 0;

            proxy_buffer_size  5m;
            proxy_buffers 100  10m;

            proxy_connect_timeout       600;
            proxy_send_timeout          600;
            proxy_read_timeout          600;
            send_timeout                600;


           location / {
                proxy_pass $scheme://$http_host$request_uri;
                proxy_cache            STATIC;
                proxy_cache_valid      200  1d;
                proxy_cache_use_stale  error timeout invalid_header updating  http_500 http_502 http_503 http_504;

            }
    }
}

当前的sysctl.conf:

# Increase size of file handles and inode cache
fs.file-max = 2097152

# Do less swapping
vm.swappiness = 10
vm.dirty_ratio = 60
vm.dirty_background_ratio = 2

### GENERAL NETWORK SECURITY OPTIONS ###

# Number of times SYNACKs for passive TCP connection.
net.ipv4.tcp_synack_retries = 2

# Allowed local port range
net.ipv4.ip_local_port_range = 2000 65535

# Protect Against TCP Time-Wait
net.ipv4.tcp_rfc1337 = 1

# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15

# Decrease the time default value for connections to keep alive
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15

### TUNING NETWORK PERFORMANCE ###

# Default Socket Receive Buffer
net.core.rmem_default = 31457280

# Maximum Socket Receive Buffer
net.core.rmem_max = 12582912

# Default Socket Send Buffer
net.core.wmem_default = 31457280

# Maximum Socket Send Buffer
net.core.wmem_max = 12582912

# Increase number of incoming connections
net.core.somaxconn = 65536

# Increase number of incoming connections backlog
net.core.netdev_max_backlog = 65536

# Increase the maximum amount of option memory buffers
net.core.optmem_max = 25165824

# Increase the maximum total buffer-space allocatable
# This is measured in units of pages (4096 bytes)
net.ipv4.tcp_mem = 65536 131072 662144
net.ipv4.udp_mem = 65536 131072 662144

# Increase the read-buffer space allocatable
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384

# Increase the write-buffer-space allocatable
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384

# Increase the tcp-time-wait buckets pool size to prevent simple DOS attacks
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1

Current limits.conf:

* soft nofile 65536
* hard nofile 65536

硬件信息:8GB RAM,4个核心,Ubuntu

其他信息: 我通过代理向3个不同大小的文件1mb,2mb和5mb

发送请求

0 个答案:

没有答案