LNMP裏常見的502問題

講關於nginx的高級配置,在Apache講了很多關於“用戶認證”、“日誌”、“重定向”等等操作,當然nginx也會有相同的操作。

首先,我們把Discuz在Nginx下實現訪問,用之前的域名www.test.com

進入目錄

[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錯誤呢?

首先,我們先排查Nginx的錯誤日誌:

進入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的問題解決了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章