Nginx 不断寻找index.html

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

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"

更新根据以下评论删除了对index.htm的所有引用

php nginx
4个回答
2
投票

默认情况下,

/etc/nginx/conf.d/
文件夹中的所有配置文件(包括此处的文件
/etc/nginx/conf.d/default.conf
)都是从
/etc/nginx/nginx.conf
扩展的。

在此配置中,您没有在

index
块中指定
server {}
指令。然后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查找。


1
投票

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

您应该在

index
块中添加明确的
server
语句。

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

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

请参阅此文档了解更多信息。


1
投票

问题是,尽管我自己的虚拟主机配置在

/etc/nginx/conf.d/default.conf
中,
nginx -T
(显示 nginx 加载的配置)并未显示文件已被读取。

我的

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


0
投票

这个问题也发生如下。

当您在同一 IP 上托管多个站点时,您会收到指令

listen example.com:80
server_name example.com

...

listen example2.com:80
server_name example2.com

但是,互联网上的恶意软件代码会不断扫描网络,以查找可能对您的网站进行攻击的媒介。 也就是说,安全扫描器尝试使用 IP 地址而不是域名连接到您的站点。您没有裸 IP 地址的默认配置。因此出现错误。

当您将服务器配置为使用正确的网页响应对 http://IP_ADDRESS 的恶意请求时,一系列额外的黑客尝试就会随之而来。也就是说,黑客将尝试检测服务器上安装的 CMS 套件,并利用他们的错误。

话虽这么说,nginx 响应此恶意请求(“404 未找到”)的方式似乎完全没问题。

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