502 Ubuntu 中使用 nginx 和 php7.0-fpm 的 Laravel 5.4 的错误网关

问题描述 投票:0回答:4

我在

Laravel 5.4
服务器中使用
Ubuntu 16.04
nginx
设置了
php7.0-fpm
应用程序,它给出了

502 Bad Gateway

Nginx 虚拟主机配置,

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www/html/laravel/public;
    index index.php index.html;

    # Make site accessible from http://localhost/
    server_name localhost;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        include                  fastcgi_params;
        fastcgi_keep_conn on;
        fastcgi_index            index.php;
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php7.0-fpm.sock;
    }
}

尝试了以下方法,但还是不行,

更改为

fastcgi_pass unix:/var/run/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;

更改为

try_files   $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;

每次更改后重新启动服务,

service nginx restart
service php7.0-fpm restart

使用此配置我只能访问主路由,

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/html/laravel/public;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}
php laravel ubuntu nginx laravel-5
4个回答
16
投票

使用默认的基于 php 的配置更新一行已成功,

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/html/laravel/public;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name localhost;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

这里把

try_files $uri $uri/ =404;
改为
try_files $uri $uri/ /index.php?$query_string;


13
投票

首次安装:

sudo apt install php-fpm

然后检查

/etc/php/7.4/fpm
,确保您的 php7.0-fpm.sock、php7.4-fpm.sock 或 7.x

版本
fastcgi_pass unix:/run/php/php7.4-fpm.sock;

2
投票

以我为例

  1. 我将项目文件夹的所有者更改为 www-data: $ sudo chown -R www-data:www-data ./my_project_folder

  2. 然后在nginx.conf文件/etc/nginx/中更改nginx本身的用户(这取决于你的nginx版本)

它对我有用。


0
投票

您只需将

user www-data;
添加到
/etc/nginx/nginx.conf
的第一行即可。这意味着 nginx 配置将由 php-fpm 的所有者(即
www-data
用户)执行。

© www.soinside.com 2019 - 2024. All rights reserved.