【问题标题】:Laravel Nginx 502 Bad GatewayLaravel Nginx 502 错误网关
【发布时间】:2020-04-06 20:47:12
【问题描述】:

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

以下代码 sn -p 显示默认站点配置文件。

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 文件。

【问题讨论】:

    标签: laravel nginx nginx-config


    【解决方案1】:

    cut defatult_server.unblock 我在下面给出的部分

    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
    

    【讨论】:

      【解决方案2】:

      你应该从这个配置文件中启用 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;
        #}
      }
      

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 2020-08-27
        • 2011-05-14
        • 2019-06-05
        • 2015-08-10
        • 2021-11-19
        • 2012-09-25
        • 2014-12-07
        • 2020-09-29
        相关资源
        最近更新 更多