-
配置nginx映射到項目代碼,能夠通路項目代碼public下的index.html,但是不能通路index.php:
cat /etc/nginx/nginx.conf 發現其中含一句:include /etc/nginx/conf.d/*.conf;
切換到目錄 cd /etc/nginx/conf.d/
建立: vim test2.conf
其中加入:
server {
listen 80;
server_name www.test2.com test2.com;
index index.html index.htm index.php;
server {
listen 80;
server_name www.test2.com test2.com;
index index.html index.htm index.php;
root /home/vagrant/test2/www/public;
set $root_path '/home/vagrant/test2/www/public';
root $root_path;
#支援如laravel架構中的www.test2.com/test/index格式的請求連接配接
location / {
try_files $uri $uri/ /index.php?$query_string;
}
#以下代碼可以設定nginx支援PHP解析
location ~ \.php$ {
root /home/vagrant/test2/www/public;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
}
}
其中比較坑人的是,開始配置為:
fastcgi_pass unix:129.0.0.1:9000
開始按照網上說的方式挨個調試:
1.檢視nginx程序:ps –aux | grep nginx
殺死多餘的占用nginx的程序:kill -9 pid
并沒有用,再試其它方法
2.檢視php-fpm是否在運作:
service php7.1-fpm status
是active的
3.檢視9000端口運作情況:
netstat -ant | grep 9000
發現沒有啟動,則9000端口沒被nginx中映射使用到,說明這裡出錯了fastcgi_pass unix:129.0.0.1:9000
這裡是由php-fpm配置決定使用的,則檢視php-fpm配置
4.cat /etc/php/7.1/fpm//php-fpm.conf
發現是引入:include=/etc/php/7.1/fpm/pool.d/*.conf
則檢視/etc/php/7.1/fpm/pool.d/下的www.conf
發現其中監聽的方式為:
listen = /run/php/php7.1-fpm.sock
5.找到問題,修改test2.conf:
vim /etc/nginx/conf.d/test2.conf
fastcgi_pass 127.0.0.1改為fastcgi_pass unix:/run/php/php7.1-fpm.sock;
6.平滑重載nginx
service nginx reload
7.重新開機php-fpm
service php7.1-fpm restart
解決問題!
說明以上隻是本人遇到的502錯誤的解決方法,502錯還可能還會是其他原因造成的,主要入手點在于檢查nginx,php-fpm配置;
另外502的錯誤還可能如下(可能還有其他情況,先列出三種):
1.檢視目前php FastCGI程序數比預設的小:
netstat -anpo | grep "php-cgi" | wc –l
若實際使用的Fascgi程序數 接近或小于 預設的 Fascgi程序數,那麼則需要增大,注意這個參數為在php-fpm.conf中的參數:max_children的大小;
2.php單個請求逾時時間設定過小:
php-fpm.conf中.request_terminate_timeout設定過小,在單個請求的php程式未執行完的情況下nginx就與php-fpm斷掉,導緻發送502 bad getway錯誤;
将其設定為0的話表示:永不逾時;
3.php程式執行時間超過Nginx等待時間:
将nginx.conf中的Fastcgi對應的fastcgi_connect_timeout,fastcgi_sebd_timeout, fastcgi_read_timeout三個參數适當增大;