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

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

零)Nginx:html

error_log /data/logs/nginx_error.log notice;java

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

worker_rlimit_nofile 51200;nginx

PHP:服務器

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

rlimit_files = 30000併發

[www]socket

request_slowlog_timeout = 2tcp

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上限。

相關文章
相關標籤/搜索