這幾天web經常出現Nginx 502的問題,先開始也像很多人一樣認為是Nginx的問題,從網上查了查原來是php-fpm在作怪。
web使用的是nginx+php的架構,網站上線還沒多久,所以最佳化方面基本只是做了些初始的配置。
查看php-fpm.log發現有警告,這些警告和網站的掛了個時間基本吻合。我就從這裡開始入手。
先開始也是找了些文檔,但是第二天還是出現問題。後來查看設定檔並翻譯了下(百度),英文底子不好。pm模組類似apache的模組,是分靜態和動態。
網上說的很多調整都是基於動態居多,但是並沒說麼定義這個模組。所以大家用動態和靜態還是要仔細看看設定檔(/usr/local/php/etc/php-fpm.conf)
pm = static
; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 300
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
;pm.start_servers = 50
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
;pm.min_spare_servers = 20
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
;pm.max_spare_servers = 500
; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
pm.process_idle_timeout = 10s;
; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 10240
紅色欄位就是定義方式的,定義好這個再去根據伺服器情況設定參數
假如使用靜態 pm.max_children這個參數會起作用,其餘不會。動態反之。
2G記憶體pm.max_children大概開啟50左右,按照實際情況來調優,這個是很必要的。
補充:
1.php-fpm進程數不夠用
使用 netstat -napo |grep "php-fpm" | wc -l 查看一下當前fastcgi進程個數,如果個數接近conf裡配置的上限,就需要調高進程數。
但也不能無休止調高,可以根據伺服器記憶體情況,可以把php-fpm子進程數調到100或以上,在4G記憶體的伺服器上200就可以。
2. 調高調高linux核心開啟檔案數量
可以使用這些命令(必須是root帳號)
echo 'ulimit -HSn 65536' >> /etc/profile
echo 'ulimit -HSn 65536' >> /etc/rc.local
source /etc/profile
3.指令碼執行時間逾時
如果指令碼因為某種原因長時間等待不返回 ,導致新來的請求不能得到處理,可以適當調小如下配置。
nginx.conf裡面主要是如下
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
php-fpm.conf裡如要是如下
request_terminate_timeout = 10s
4.緩衝設定比較小
修改或增加配置到nginx.conf
proxy_buffer_size 64k;
proxy_buffers 512k;
proxy_busy_buffers_size 128k;
5. recv() failed (104: Connection reset by peer) while reading response header from upstream
可能的原因機房網路丟包或者機房有硬體防火牆禁止訪問該網域名稱
但最重要的是程式裡要設定好逾時,不要使用php-fpm的request_terminate_timeout,
最好設成request_terminate_timeout=0;
因為這個參數會直接殺掉php進程,然後重啟php進程,這樣前端nginx就會返回104: Connection reset by peer。這個過程是很慢,總體感覺就是網站很卡。
May 01 10:50:58.044162 [WARNING] [pool www] child 4074, script '/usr/local/nginx/html/quancha/sameip/detail.php' execution timed out (15.129933 sec), terminating
May 01 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal 15 SIGTERM after 90.227060 seconds from start
May 01 10:50:58.046818 [NOTICE] [pool www] child 4082 started
說一千道一萬最重要的就是程式裡控制好逾時,gethostbyname、curl、file_get_contents等函數的都要設定逾時時間。
另一個就是多說,這個東西是增加了網站的互動性,但是使用的多了反應就慢了,如果你網站逾時且使用了多說是,可以關閉它。
如果哪裡有不足希望大家提意見,502解決辦法。