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

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

当我尝试仅通过端口 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;
        #}
}

里面/var/www/default有一个名为 index.html 的文件。现在到了奇怪的部分。如果我将监听端口更改为nginx,则正确提供 index.html。我只有 3 个站点,其他站点是虚拟主机,因此它们没有监听指令。我暂时8888将文件夹设置/var/www/default为just,仍然返回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默认根的配置在哪里?

答案1

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

因此,请尝试以下作为您的聆听线路:

listen 80 default_server;

default_server然后,如果其他虚拟主机有该定义,您可能必须将其删除。

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

相关内容