天天看點

nginx的502問題

1.配置錯誤

因為nginx找不到php-fpm了或者socket檔案權限不對,是以報錯,一般是nginx的配置檔案中fastcgi_pass後面的路徑配置不當,後面可以是socket或者是ip:port,從php5.4版本之後如果使用socket方式,預設權限給的比較低,是以需要把監聽權限改為777,對應配置為 listen.mode=0777

2.資源耗盡

lnmp架構在處理php時,nginx直接調取後端的php-fpm服務,如果nginx的請求量偏高,我們又沒有給php-fpm配置足夠的子程序,那麼php-fpm就會資源耗盡,一旦資源耗盡nginx找不到php-fpm就會出現502錯誤

解決方案:

去調整php-fpm.conf中的pm.max_children數值,使其增加,但是也不能無限增加,畢竟資源有限,一般4G記憶體機器如果跑php-fpm和nginx,不跑mysql可以設定為150,8G為300以此類推!

3.除了上面的兩種錯誤還有其他的原因很少有,我們可以借助nginx的錯誤日志來進行排查

vim /usr/local/nginx/logs/nginx_error.log  我們也可以給日志定義級别vim/usr/local/nginx/conf/nginx.conf 找到error_log,預設是crit最嚴謹的就行,也可以改成debug顯示的資訊最全面,但是很容易撐爆我們的磁盤。

如下為:nginx解析php相關配置

<code>[root@chy01 vhost]</code><code># vim test.com.conf </code>

<code>   </code><code>location ~ \.php$</code>

<code>            </code><code>{</code>

<code>            </code><code>include fastcgi_params;</code>

<code>            </code><code>fastcgi_pass unix:</code><code>/tmp/php-fcgi</code><code>.sock;</code>

<code>            </code><code>fastcgi_index index.php;</code>

<code>            </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/data/wwwroot/test</code><code>.com/$fastcgi_script_name;</code>

<code>       </code><code>}</code>

<code>(如上在虛拟主機裡面配置的php解析,需要注意幾個地方1fastcgi_pass unix:</code><code>/tmp/php-fcgi</code><code>.sock; 這裡的路徑不要寫錯,如果寫錯會出現502問題,</code>

<code>[root@chy01 conf]</code><code># cat /usr/local/php-fpm/etc/php-fpm.conf</code>

<code>[global]</code>

<code>pid = </code><code>/usr/local/php-fpm/var/run/php-fpm</code><code>.pid</code>

<code>error_log = </code><code>/usr/local/php-fpm/var/log/php-fpm</code><code>.log</code>

<code>[www]</code>

<code>listen = </code><code>/tmp/php-fcgi</code><code>.sock (這個路徑要與nginx虛拟主機的路徑完全保持一緻)</code>

<code>listen.mode = 666</code>

<code>user = php-fpm</code>

<code>group = php-fpm</code>

<code>pm = dynamic</code>

<code>pm.max_children = 50</code>

<code>pm.start_servers = 20</code>

<code>pm.min_spare_servers = 5</code>

<code>pm.max_spare_servers = 35</code>

<code>pm.max_requests = 500</code>

<code>rlimit_files = 1024</code>

<code>還有另一種情況就是php監聽的不是sock,而是ip加端口的情況時   </code>

<code>location ~ \.php$</code>

<code>            </code><code>{ </code>

<code>            </code><code>#fastcgi_pass unix:/tmp/php-fcgi.sock;</code>

<code>            </code><code>fastcgi_pass 127.0.0.1:9000</code>

<code>       </code><code>}    </code>

<code>這時fastcgi_pass 127.0.0.1:9000 這裡就需要這樣操作,這的ip要與php配置檔案裡面監聽的位址保持一緻)</code>

<code>第2個需要注意的地方:fastcgi_param SCRIPT_FILENAME </code><code>/data/wwwroot/test</code><code>.com/$fastcgi_script_name; 這裡的路徑要與server 下的</code>

<code>{</code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name </code><code>test</code><code>.com test2.com lll.com;</code>

<code>    </code><code>index index.html index.htm index.php admin.php;</code>

<code>    </code><code>root </code><code>/data/wwwroot/test</code><code>.com; 這個路徑保持一緻。</code>

<code>    </code><code>if</code> <code>($host != </code><code>'test.com'</code> <code>) {</code>

<code>       </code><code>rewrite  ^/(.*)$  http:</code><code>//test</code><code>.com/$1  permanent;</code>

<code>    </code><code>}</code>

<code>3.php5.4之後的版本有個特性:</code>

<code>[root@chy01 vhost]</code><code># vi /usr/local/php-fpm/etc/php-fpm.conf (在配置檔案中)</code>

<code>listen = </code><code>/tmp/php-fcgi</code><code>.sock</code>

<code>(如果監聽的是sock就必須要監聽mode)</code>

<code>#listen.mode = 666</code>

<code>[root@chy01 vhost]</code><code># ls -l /tmp/php-fcgi.sock </code>

<code>srw-rw---- 1 root root 0 8月  15 03:02 </code><code>/tmp/php-fcgi</code><code>.sock</code>

<code>(可以看到屬主屬組都為root,并且沒有讀的權限)</code>

<code>[root@chy01 vhost]</code><code># curl -x127.0.0.1:80 test.com/admin.php -I</code>

<code>HTTP</code><code>/1</code><code>.1 502 Bad Gateway</code>

<code>Server: nginx</code><code>/1</code><code>.12.1</code>

<code>Date: Mon, 14 Aug 2017 19:03:48 GMT</code>

<code>Content-Type: text</code><code>/html</code>

<code>Content-Length: 173</code>

<code>Connection: keep-alive</code>

<code>(當在curl時會發現報502)</code>

<code>[root@chy01 vhost]</code><code># tail -n3 /usr/local/nginx/logs/nginx_error.log </code>

<code>2017</code><code>/08/14</code> <code>22:24:06 [crit] 3684</code><code>#0: *7 connect() to unix:/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/php-fcgi.sock:", host: "test.com"</code>

<code>2017</code><code>/08/15</code> <code>03:03:48 [crit] 3682</code><code>#0: *5 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"</code>

<code>2017</code><code>/08/15</code> <code>03:06:03 [crit] 3682</code><code>#0: *7 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"</code>

<code>(檢視錯誤日志,Permission denied發現拒絕,沒有權限。是因為它的屬主,屬組為nobody ,此時如果将</code><code>/tmp/php-fcgi</code><code>.sock 的權限設定為nobody就沒有任何問題了)</code>

<code>[root@chy01 vhost]</code><code># ps aux |grep nginx</code>

<code>root       3485  0.0  0.1  21272  1652 ?        Ss   02:28   0:00 nginx: master process </code><code>/usr/local/nginx/sbin/nginx</code> <code>-c </code><code>/usr/local/nginx/conf/nginx</code><code>.conf</code>

<code>nobody     3682  0.0  0.2  23152  3796 ?        S    03:01   0:00 nginx: worker process</code>

<code>nobody     3683  0.0  0.2  23152  3300 ?        S    03:01   0:00 nginx: worker process</code>

<code>root       3791  0.0  0.0 112664   972 pts</code><code>/0</code>    <code>S+   03:11   0:00 </code><code>grep</code> <code>--color=auto nginx</code>

<code>(還有一種情況也是502就是nginx的資源耗盡))</code>

nginx的502問題

     本文轉自我不是瘦子51CTO部落格,原文連結:http://blog.51cto.com/chy940405/1980650,如需轉載請自行聯系原作者