【问题标题】:Index and root pages are not working, 502 Bad Gateway索引和根页面不工作,502 Bad Gateway
【发布时间】:2020-07-02 16:41:20
【问题描述】:

URL example.org/index.php(和任何example.org/etc.php失败,但example.org/foo 工作正常,适用于任何foo 文件夹。

server {
        server_name example.org example.com;
        access_log /var/log/nginx/example.org.access_log;
        root /var/www/example.org/;
        index  index.php index.html index.htm;

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

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

  • nginx 版本:1.17.10 (Ubuntu)
  • ? /var/log/nginx/error.log(没有特定于服务器?)

sudo tail /var/log/nginx/error.log 什么都不显示,只显示其他站点的错误,

...
2020/06/19 12:58:02 [error] 871296#871296: *23 "/var/www/example2.org/index2.php" is not found (2: No such file or directory), client: 181.177.112.xx, server: example2.org, request: "GET / HTTP/1.0", host: "example2.org"

解决方案的主要线索(见我的回答)

文件/run/php/php7.0-fpm.sock不存在,所以失败在fastcgi_pass unix:/run/php/php7.0-fpm.sock;

ls -l /run/php/*.sock 显示

lrwxrwxrwx 1 root     root     30 May 17 05:18 /run/php/php-fpm.sock -> /etc/alternatives/php-fpm.sock
srw-rw---- 1 www-data www-data  0 May 29 06:40 /run/php/php7.4-fpm.sock

Ideal 是一个通用套接字,即php-fpm.sock,但它似乎不一样(不重定向到php7.4-fpm.sock?)。

【问题讨论】:

  • 每个server {} 块都应该有一个listen。例如:listen 80;listen 443
  • 嗨@0stone0,没有意义,正如我在问题中所说,它适用于/foo 文件夹:listen 错误会影响文件夹和根 URL。 listen 80/etc/nginx/sites-available/default 原始 NGINX 脚本上,看起来工作正常
  • 您可以发布一些来自 error.log 和 /var/log/nginx/example.org.access_log 的日志吗?
  • 嗨@DilsonRainov,文件access_log 在那里,没有相关信息进入,但是error.log 在哪里? 是通用/var/log/nginx/error.log 还是有一个特定于域?
  • 好的。你能 ls 文件 /run/php/php7.0-fpm.sock 吗?看起来 php-fpm 没有启动。

标签: nginx


【解决方案1】:

Ideal 是一个通用套接字,但目前使用的是php7.4-fpm.sock

server {
        server_name example.org example.com;
        ...
        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }
}

此解决方案并不完美(PHP 更改版本时出现大问题)但运行良好。

【讨论】:

  • 您可以切换到 php-fpm 端口,但这会导致速度略有下降。这里有一点解释:tecmint.com/connect-nginx-to-php-fpm
  • 嗨@DilsonRainov,感谢链接(!)......嗯......它只说我的明确版本unix:/run/php/php7.4-fpm.sock是正确的,而替代unix:/run/php-fpm/www.sock仅适用于CentOS...你能告诉我最包含哪一行吗?现在我的答案是一个维基,你可以编辑。
【解决方案2】:

如果您正在寻找通用解决方案,因此您不必担心可以尝试的 PHP 版本:

server {
        server_name example.org example.com;
        ...
        location ~ \.php$ {
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
        }
}

并检查文件/etc/php/7.*/fpm/php.ini 中是否写入了以下行:

cgi.fix_pathinfo=0

【讨论】:

    【解决方案3】:

    试试这个(适应你的需要):

    server {
        listen 80;
        server_name example.org;
        root /var/www/example.org; //If Laravel /var/www/example.org/public
             
        index index.html index.htm index.php etc.php;
    
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        access_log off;
        error_log  /var/log/nginx/example.org-error.log error;
    
        error_page 404 /index.php;
    
        sendfile off;
    
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }
    
        location ~ /\.ht {
            deny all;
        }
        
        client_max_body_size 1000m;
    }
    

    注意事项:

    1. server_name (doc) 变量保存值example.org。该值必须存在于hosts 文件中。像这样127.0.0.1 example.org
    2. root 变量中,NginX 期望找到系统入口点/var/www/example.org/ 的路径。这里example.org 是您项目的文件名。如果您的入口点文件是index.phpetc.php,您必须确认这些文件存在于您的根目录中。像这样的东西:/var/www/example.org/index.php/var/www/example.org/etc.php。如果您将入口点放在另一个文件夹中,它将不起作用。例如,如果您将变量 root 设置为 /var/www/example.org/public(如 Laravel),您应该将入口点文件放在 public 文件夹中,而不是在 example.org 文件夹中
    3. index 变量是 NginX 期望找到的 root 中可能的入口点文件的列表。在你的情况下,你应该有这样的东西:index index.html index.htm index.php etc.php
    4. php 的位置。如果您将 NginX variable 设置为 /var/run/php/php7.0-fpm.sock,则必须确认文件 php7.0-fpm.sock 已存在于 /var/run/php/
    5. 如果你想向你的服务器执行上传过程,我把client_max_body_sizefastcgi_buffer_sizefastcgi_buffers 放了。如果不是,则这些变量不是必需的。

    最后是错误:

    1. example2.org/index2.php" 未找到。似乎 NginX 期望在根文件夹 example2.org/ 内找到 index2.php 入口点文件。请确保那里有此文件。
    2. 我不明白您使用的是 php 7.0 还是 7.4。无论如何,您必须传递您的php7.x-fpm 文件的位置。只需进入文件夹并确认它们已经存在即可。
    3. 最后,您可能面临的另一个可能的错误是权限。确保www-data(NginX 用户)对您的文件具有适当的 WRX 权限。
    4. Here 你会发现 NginX 是如何处理请求的。
    5. 还有here NginX 使用的变量的综合列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 2013-06-12
      • 2020-09-01
      • 2013-08-04
      • 2018-04-14
      • 2020-08-27
      • 2020-04-05
      • 2020-07-12
      相关资源
      最近更新 更多