nginx 默认 root 在端口 80 上返回 404

Alb*_*n N 3 nginx

当我尝试仅通过端口 80 上的 IP 访问我的服务器时遇到问题。

如果我访问该 IP,我会收到 nginx 404 错误页面。

这是我的默认配置:

# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts to this file


server {
        listen    80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6
        # Document root
        root /var/www/default;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/www;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}
Run Code Online (Sandbox Code Playgroud)

里面/var/www/default有一个名为index.html的文件。现在到奇怪的部分。如果我将侦听端口更改为8888nginx,则可以正确服务 index.html。我只有 3 个站点,其他都是虚拟主机,因此它们没有监听指令。我暂时将/var/www/default文件夹设置为,仍然在 port 上返回。chmod 77740480

编辑: 我检查了 nginx 的错误日志,内容如下: 2014/04/08 09:30:21 [error] 3349#0: *1 "/etc/nginx/html/index.html" is not found

那么问题来了,这个表示/etc/nginx/html/index.html默认根的配置在哪里?

Ter*_*nen 5

default_server您的指令没有选择权listen。因此,该请求与您的 nginx 配置中的其他虚拟主机相匹配。

所以,试试这个作为你的监听线:

listen 80 default_server;
Run Code Online (Sandbox Code Playgroud)

然后,您可能必须default_server从其他虚拟主机中删除(如果它们具有该定义)。

127.0.0.1另一件事可能是,您是否使用或访问服务器localhostserver_name localhost表示仅localhost在 URL 中使用时才使用虚拟主机。