Laravel Nginx 502错误网关

时间:2019-12-13 11:39:23

标签: laravel nginx nginx-config

我尝试使用Nginx Web服务器部署Laravel应用程序。但是,我收到502 Bad Gateway错误。我已经检查了位于sites-available文件夹中的默认文件,并进行了一些更改。

以下代码段显示了默认的站点配置文件。

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/laravel/skill-tool-app/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name 18.130.196.144;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404 /index.php?$query_string;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
                #fastcgi_pass 127.0.0.1:9000;
        #}

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

我也检查了文件权限。当我尝试访问该网站时。它使浏览器下载index.php文件。

2 个答案:

答案 0 :(得分:1)

cut defatult_server。取消阻止我在下面提供的部分

server {
  listen 80;
  listen [::]:80;

  root /var/www/html/laravel/skill-tool-app/public;

  # Add index.php to the list if you are using PHP
  index index.php index.html index.htm index.nginx-debian.html;

  server_name 18.130.196.144;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404 /index.php?$query_string;
  }

  # pass PHP scripts to FastCGI server
  #
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #fastcgi_pass 127.0.0.1:9000;
  }

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

现在您需要启用配置,对已启用的站点进行符号链接:

ln -s /etc/sites-available/YOUR_FILE_NAME /etc/sites-enabled/

然后

sudo systemctl reload nginx

service nginx restart

答案 1 :(得分:0)

您必须从此配置文件启用php:

请更新您的位置信息块:

location ~ \.php$ {
include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    # PLEASE UPDATE php version with your current installed version
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #fastcgi_pass 127.0.0.1:9000;
}              

整个文件应如下所示:

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/html/laravel/skill-tool-app/public;

  # Add index.php to the list if you are using PHP
  index index.php index.html index.htm index.nginx-debian.html;

  server_name 18.130.196.144;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404 /index.php?$query_string;
  }

  # pass PHP scripts to FastCGI server
  #
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #fastcgi_pass 127.0.0.1:9000;
  }

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