1. 程式人生 > >nginx,php 502錯誤解決

nginx,php 502錯誤解決

一:錯誤場景:leyangjun-cc-web.com/api/cc/callback.php 訪問返回502

 

二:Nginx錯誤報錯為:

2018/10/22 18:07:09 [crit] 775#0: *41 connect() to unix:/var/run/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: leyangjun-cc-web.com, request: "GET /aj/cc/callback HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7.0-fpm.sock:", host: "leyangjun-cc-web.com"

 

三:錯誤原因

我的Nginx配置檔案,配置的不對

server {
    listen       80;
    server_name  leyangjun-cc-web.com;
    root   /www/leyanjgun/cc/src/App/;

    index  index.html index.htm index.php;

    access_log /www/log/dev-cc.leyangjun.com-access.log;
    error_log  /www/log/dev-cc.leyangjun.com-error.log error;

    location / {
         try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php7.0-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
        #include     /usr/local/etc/nginx/php-fpm;
        include    fastcgi_params;
    }

    error_page  404 403 500 502 503 504  /error.html;
    location = /error.html {
       root   /Users/leyangjun/code/;
    }
}

就是因為 fastcgi_pass unix:/run/php7.0-fpm.sock; 這個配置,Nginx走的是sock配置,但是我php的www.conf配置檔案中沒有對sock檔案監聽,導致Nginx報錯

解決方法:

一:vi /usr/local/etc/php/7.1/php-fpm.d/www.conf 增加對檔案的監聽,能後儲存重啟服務即可

二:或者直接改用IP形式使用

server {
    listen       80;
    server_name  leyangjun-cc-web.com;
    root   /www/leyanjgun/cc/src/App/;

    index  index.html index.htm index.php;

    access_log /www/log/dev-cc.leyangjun.com-access.log;
    error_log  /www/log/dev-cc.leyangjun.com-error.log error;

    location ~ ^/(page|aj|api|test)/{
         try_files $uri $uri/ /index.php?$query_string;
    }

    location / {
        try_files /index.html =404;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param MY_ENV 'develop';
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}     

儲存重啟Nginx服務即可