【问题标题】:Nginx keeps looking for index.htmlNginx 一直在寻找 index.html
【发布时间】:2020-06-22 21:40:14
【问题描述】:

http://localhost 显示 nginx 1.13 的 404。当我查看容器日志时,我可以看到 nginx 没有将请求传递给 php-fpm,而是在寻找 index.html。 我不知道为什么它不会将请求传递给 php-fpm

/etc/nginx/conf.d/default.conf

我已验证此文件已加载。

server {
    listen   80;
    root /var/www/html/public;
    index index.php;

    charset utf-8;

    # look for local files on the container before sending the request to fpm
    location / {
        try_files $uri /index.php?$query_string;
    }

    # nothing local, let fpm handle it
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass            localhost:9000;
        fastcgi_index           index.php;
        include                 fastcgi_params;
        fastcgi_param           REQUEST_METHOD  $request_method;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param           QUERY_STRING    $query_string;
        fastcgi_param           CONTENT_TYPE    $content_type;
        fastcgi_param           CONTENT_LENGTH  $content_length;
        # Httpoxy exploit (https://httpoxy.org/) fix
        fastcgi_param           HTTP_PROXY "";

        # allow larger POSTS for handling stripe payment tokens
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
   }
}

Web 容器内的进程列表:

PID   USER     TIME   COMMAND
    1 root       0:00 s6-svscan -t0 /var/run/s6/services
   33 root       0:00 s6-supervise s6-fdholderd
  170 root       0:00 s6-supervise php-fpm
  171 root       0:00 s6-supervise nginx
  173 root       0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
  174 root       0:00 {run} /bin/sh ./run
  177 root       0:00 nginx: master process nginx -g daemon off;
  187 nginx      0:00 nginx: worker process
  192 www-data   0:00 php-fpm: pool www
  193 www-data   0:00 php-fpm: pool www
  194 root       0:00 ps -ef

容器日志

web_1    | 2017/05/13 06:13:10 [error] 187#187: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", host: "mysite.local"
web_1    | 172.19.0.1 - - [13/May/2017:06:13:10 +0000] "GET / HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"

更新删除了以下几个 cmets 对 index.htm 的所有引用

【问题讨论】:

  • 错误信息表明这个server 块没有处理请求。通过验证您的意思是nginx -T?要么删除默认服务器块,要么给它一个server_name
  • 默认有server_name "",即。你永远不会得到这个带有非空Host 标头的块,除非指定了default_server

标签: php nginx


【解决方案1】:

nginx 正在使用 index 指令的默认值来处理 URI /(因为该目录确实存在)。

您应该在 server 块中添加一个显式的 index 语句。

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

    # nothing local, let fpm handle it
    location ~ [^/]\.php(/|$) {
        ...
    }
}

请参阅this document 了解更多信息。

【讨论】:

    【解决方案2】:

    问题是尽管我自己的 vhosts 配置在 /etc/nginx/conf.d/default.conf 中,nginx -T(显示 nginx 的加载配置)没有显示文件已被读取。

    我的nginx.conf 中缺少include /etc/nginx/conf.d/*.conf; 指令。

    【讨论】:

      【解决方案3】:

      默认情况下,/etc/nginx/conf.d/ 文件夹中的所有配置文件,包括您在此处的文件/etc/nginx/conf.d/default.conf,都是从/etc/nginx/nginx.conf 扩展而来的。

      在此配置中,您没有在 server {} 块中指定 index 指令。然后nginx会在/etc/nginx/nginx.conf中查找默认值。

      解决方案是覆盖默认值:

      server {
          listen   80;
          root /var/www/html/public;
          charset utf-8;
          index index.php index.html index.htm;
      }
      

      然后重置你的 nginx:sudo service nginx restart

      那么index.php 的优先级会比其他的更高,以便 nginx 查找。

      【讨论】:

      • 感谢您的反馈。在实施该更改并通过运行docker-compose exec web cat /etc/nginx/conf.d/default.conf 验证它是否在文件系统中之后,我仍然看到问题。
      • 你能给我/etc/nginx/conf.d/default.conf文件的内容吗?最好在您的容器内登录并检查我提到的文件。因为更改后您还需要刷新您的 nginx。
      • 我已经更新了上面的配置。在对虚拟主机进行配置更改后,我由容器重新启动。 vhost 文件是通过挂载提供的,因此足以更新它。
      • 如果是这样,您可以将原始文件挂载到该容器之前,然后重新加载容器使其生效。因为我们现在在 nginx 上遇到问题,所以你需要这样做。
      猜你喜欢
      • 2018-09-24
      • 2017-08-16
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2021-10-27
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多