01月13, 2018

nginx单机优化

目标:

nginx并发:5000+
nginx+php并发:3000+
nginx+php+mysql并发:2000+
nginx+php+mysql+memcache并发:1500+

1. ab

ab安装:yum -y install httpd-tools
ab -c -n
说明:c并发数 n请求总数
./configure --help|grep status

2. 安装统计模块

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=/app/ngx_http_consistent_hash-master
make && make install

nginx.conf
location /status {
    stub_status on;
    access_log off;
}

3. 优化

3.1 系统层面

允许等待中的监听
echo 5000 > /proc/sys/net/core/somaxconn
快速tcp回收
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
tcp连接重用
echo 1 > /proc/sys/net/ipv4/tcp_twuse
关闭洪水抵御
echo 0 > /proc/sys/net/ipv4/tcp_syncookies

ulimit -n 65535 每个进程可以打开的文件数
进程最大文件描述符         
    /etc/security/limits.conf 
    soft nofile 50000  
    hard nofile 50000

3.2 nginx配置

work process  打开的进程数量,为CPU核心数即可
worker_rlimit_nofile 30000; 进程打开文件的最大数量 
keepalive_timeout  0; 请求完成后不保留tcp连接

本文链接:https://blog.hijs.cc/post/nginx-optimize.html

-- EOF --

Comments