[效果不錯] nginx 高並發參數配置及linux內核參數優化,完整的內核優化設置。PHP-FPM高負載解決辦法。


背景:對vps小資源的實踐中對,https://justwinit.cn/post/7536/ 的再優化,再實踐,再優化,特別是Nginx,PHP,內核:

零)Nginx:

error_log /data/logs/nginx_error.log notice;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

PHP:

我的1g測試機,開64個是最好的,建議使用壓力測試獲取最佳值

rlimit_files = 30000

[www]

request_slowlog_timeout = 2

slowlog = /data/logs/php/slow.log

pm.max_children = 64

—————————————————————加上下面的內核優化———————————————————————————

一)nginx 高並發參數配置及linux內核參數優化 :

(1) vi /etc/sysctl.conf CentOS5.5中可以將所有內容清空直接替換為如下內容:

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 68719476736

kernel.shmall = 4294967296

net.ipv4.tcp_max_tw_buckets = 6000

net.ipv4.tcp_sack = 1

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_rmem = 4096 87380 4194304

net.ipv4.tcp_wmem = 4096 16384 4194304

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.core.somaxconn = 262144

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

net.ipv4.ip_local_port_range = 1024 65000

使配置立即生效可使用如下命令:

/sbin/sysctl -p

(2)關於系統連接數的優化:

linux 默認值 open files 和 max user processes 為1024

#ulimit -n

1024

#ulimit –u

1024

問題描述: 說明 server 只允許同時打開 1024 個文件,處理 1024個用戶進程

使用ulimit -a 可以查看當前系統的所有限制值,使用ulimit -n 可以查看當前的最大打開文件數。

新裝的linux 默認只有1024 ,當作負載較大的服務器時,很容易遇到error: too many open files。因此,需要將其改大。

解決方法:

使用 ulimit –n 65535 可即時修改,但重啟后就無效了。(注ulimit -SHn 65535 等效 ulimit-n 65535 ,-S 指soft ,-H 指hard)

有如下三種修改方式:

1. 在/etc/rc.local 中增加一行 ulimit -SHn 65535

2. 在/etc/profile 中增加一行 ulimit -SHn 65535

3. 在/etc/security/limits.conf最后增加:

* soft nofile 65535

* hard nofile 65535

* soft nproc 65535

* hard nproc 65535

具體使用哪種,在 CentOS 中使用第1 種方式無效果,使用第3 種方式有效果,而在Debian 中使用第2種有效果

# ulimit -n

65535

# ulimit -u

65535

備注:ulimit 命令本身就有分軟硬設置,加-H 就是硬,加-S 就是軟默認顯示的是軟限制

soft 限制指的是當前系統生效的設置值。 hard 限制值可以被普通用戶降低。但是不能增加。 soft 限制不能設置的比hard 限制更高。 只有 root 用戶才能夠增加 hard 限制值。

(3)對nginx作cpu親和性綁定:

worker_cpu_affinity 00000001 0000001000000100 00001000 00010000 00100000 01000000 10000000;

我的:

worker_processes  12;

worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000 0001 0010 0100 1000;

摘自:http://blog.csdn.net/rachel_luo/article/details/8668137

二)PHP-FPM高負載解決辦法(我的1g測試機,開64個是最好的,建議使用壓力測試獲取最佳值):

1.盡量少安裝PHP模塊,最簡單是最好(快)的

2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);

2.把您的PHP FastCGI子進程數調到100或以上,在4G內存的服務器上200就可以

注:我的1g測試機,開64個是最好的,建議使用壓力測試獲取最佳值

3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;

3.使用socket連接FastCGI,linux操作系統可以放在 /dev/shm中

注:在php-fpm.cnf里設置<value name=”listen_address”>/tmp/nginx.socket</value>就可以通過socket連接FastCGI了,/dev/shm是內存文件系統,放在內存中肯定會快了.記得這時也要在nginx里的配置里進行修改,保持一致.

location ~ .*\.(php|php5)?$

{

#將Nginx與FastCGI的通信方式由TCP改為Unix Socket。TCP在高並發訪問下比Unix Socket穩定,但Unix Socket速度要比TCP快。

fastcgi_pass  unix:/tmp/php-cgi.sock;

#fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

摘自:http://www.blogjava.net/tbwshc/archive/2012/08/16/385599.html

三)nginx與php-fpm 打開文件過多(Too many open files)Too many open files

經過第2)步sysctl -p設置后,一般不會出現上面第三)的問題:

[root@jackxiang conf]# ulimit -Hn

65535

[root@jackxiang conf]# ulimit -Sn

65535

PHP-FPM配置文件:

rlimit_files = 30000

NGINX配置文件:

# set open fd limit to 30000

worker_rlimit_nofile 30000;

以下是上面的詳細解決方案:

操作系統

先查看 Linux (Cent Os)的文件打開限制

ulimit -Hn

ulimit -Sn

-H 為 Hard 解釋為硬件 -S 為 Soft 為軟件。 具體意義不是很明。

先去修改 /etc/sysctl.conf

添加或者修改

fs.file-max = 70000

修改 /etc/security/limits.conf

添加或修改

<user>   soft    nofile   10000

<user>   hard    nofile   30000

后面的 10000,30000可以根據需要調整,至於 可以根據需要修改為 對應要擴大文件打開數 的用戶,

這里 因為我要處理 nginx 和 php-fpm 2個服務對應的2個用戶,圖方便 就使用了 * ,意思就是 所有用戶。

重載 sysctrl配置

sysctl -p

PHP-FPM

修改 /etc/php-fpm.d/www.conf

添加/修改

rlimit_files = 30000

數字隨意,當然要低於OS的設置。

NGINX

修改 /etc/nginx/nginx.conf

最外層 我是跟在 worker_processes 之后

添加或者修改

# set open fd limit to 30000

worker_rlimit_nofile 30000;

調試測試

重啟 nginx, php-fpm

重新登錄OS

使用 ulimit 查看當前的 限制,看是否改好了

同時 使用

lsof | wc -l

統計當前打開文件的數量,如果低於你的上限,就滿足吧,記得高峰時候來查看下,隨時調整你的ulimit上限。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM