1. 程式人生 > >LNMP時,出現502 Bad Gateway的錯誤提示

LNMP時,出現502 Bad Gateway的錯誤提示

      因為工作需要,要在ubuntu中安裝LNMP環境,在這裡,php是最新版本php7.1。一切都進展得很順利,安裝完成後,在瀏覽器中輸入http://127.0.0.1/info.php,出現了502 Bad Gateway的錯誤提示。


開啟/var/log/nginx 下的error.log,發現提示為:2017/07/15 14:46:47 [error] 1228#0: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1"。

進入/etc/php/7.1/fpm/pool.d下,開啟www.conf檔案,找到listen這一行,發現listen = /run/php/php7.1-fpm.sock。(我的php7.1-fpm.sock 在/var/run/php/php7.1-fpm.sock下,於是改成了/var/run/php/php7.1-fpm.sock)


進入/etc/nginx/sites-available目錄下,開啟default檔案,裡面的location配置為:

 location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
               fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #         fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        #         include fastcgi_params;
        #         fastcgi_index index.php;
         #       include fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        }

很明顯,default檔案中的l監聽方式和www.conf檔案中的不一樣,因此我改為:

 location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_index index.php;
                include fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        }

最終default檔案如下:


儲存這個配置後,重啟Nginx ;service nginx restart,然後重新整理瀏覽器,最後的結果展示如下:


問題得到了解決!