天天看點

LNMP裡常見的502問題

講關于nginx的進階配置,在Apache講了很多關于“使用者認證”、“日志”、“重定向”等等操作,當然nginx也會有相同的操作。

進入目錄

[root@LampLinux vhosts]# cd /usr/local/nginx/conf/vhosts/

建立test.conf檔案

[root@LampLinux vhosts]# vim test.conf

寫入如下代碼(紅色部分為變更後的資訊)

server

{

    listen 80;

    server_name www.test.com;

    index index.html index.htm index.php;

    root /data/www;

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/www.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME  /data/www$fastcgi_script_name;

    }

}

重新開機Nginx

[root@LampLinux vhosts]# /usr/local/nginx/sbin/nginx -t

[root@LampLinux vhosts]# /etc/init.d/nginx reload

浏覽器輸入:www.test.com,顯示502錯誤。

我們該怎麼樣排查502錯誤呢?

<b>首先,我們先排查Nginx的錯誤日志:</b>

進入nginx配置檔案:

[root@LampLinux vhosts]# vim /usr/local/nginx/conf/nginx.conf

找到如下資訊: 

error_log /usr/local/nginx/logs/nginx_error.log crit;

可以看出紅字部分為“錯誤日志”的路徑和檔案。

檢視檔案内容:

[root@LampLinux vhosts]# cat /usr/local/nginx/logs/nginx_error.log

有很多内容,但是有一行Unix:/tmp/www.sock failed (13:permission denied),表示沒有權限去讀取檔案。

我們來檢視/tmp/www.sock檔案的權限:

[root@LampLinux vhosts]# ls -l /tmp/www.sock

 srw-rw----. 1 root  root  0 8月  12 01:19 /tmp/www.sock

那麼讀這個.sock檔案的使用者是誰?

[root@LampLinux vhosts]# ps aux |grep nginx

root     10929  0.0  0.1   5516  1448 ?        Ss   00:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nobody   11039  0.0  0.2   7112  2880 ?        S    01:16   0:00 nginx: worker process

nobody   11040  0.0  0.2   6768  2272 ?        S    01:16   0:01 nginx: worker process

root     11115  0.0  0.0   5980   756 pts/0    S+   03:06   0:00 grep nginx

我們可以看出,使用者是nobody,這一點很重要;

為什麼是nobody呢?

我們在配置檔案的時候指定了Unix:/tmp/www.sock,讓它去那裡讀取,由于沒有權限,是以它會顯示502錯誤。

怎麼讓.sock可讀?

編輯php-fpm的配置檔案:

[root@LampLinux vhosts]# vim /usr/local/php2/etc/php-fpm.conf

在php-fpm配置中的:

user = php-fpm

group = php-fpm 

                                    下面寫入配置(關于指定監聽使用者群組的人是誰?)

listen.owner = nobody

listen.group = nobody

儲存退出

:wq

重新開機php-fpm服務

[root@LampLinux vhosts]# /usr/local/php2/sbin/php-fpm -t

[root@LampLinux vhosts]# /etc/init.d/php-fpm restart

浏覽器輸入www.test.com  就沒有問題了,502的問題解決了。

本文轉自 聽丶飛鳥說 51CTO部落格,原文連結:http://blog.51cto.com/286577399/1683779

繼續閱讀