nginx:如何始终为默认主机返回自定义 404 页面

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

我配置了 nginx 0.8.53,并配置了一些可以按需要工作的虚拟主机。但是,由于 nginx 在虚拟主机上的“最佳匹配”,我需要添加一个默认主机来捕获所有不针对特定虚拟主机的请求。我希望默认主机返回我创建的自定义 404 页面,而不是默认的 nginx 404 页面。

我以为我需要类似的东西:

# The default server:
server {
    listen       80 default_server;
    server_name  everythingelse;

    # Everything is a 404
    location / {
        return 404;
    }
    error_page 404 /opt/local/html/404.html;
}

但这仍然返回默认的 nginx 404 页面。看来

return 404
忽略了
error_page
配置。

nginx
7个回答
49
投票

这是我的conf中使其工作的内容:

# The default server.
server {
  listen       80 default_server;
  server_name  everythingelse;

  error_page 404 /404.html;

  # Everything is a 404
  location / {
    return 404; #return the code 404
  }

  # link the code to the file
  location = /404.html {
    #EDIT this line to make it match the folder where there is your errors page
    #Dont forget to create 404.html in this folder
    root  /var/www/nginx/errors/;
  }
}

11
投票

nginx 中很少有指令采用文件系统路径。你想要这样的东西:

# The default server.
server {
  listen       80 default_server;
  server_name  everythingelse;

  root /opt/local/html;

  error_page 404 /404.html;

  # Everything is a 404
  location / {
    return 404;
  }

  # EDIT: You may need this to prevent return 404; recursion
  location = /404.html {
    internal;
  }
}

3
投票

将 error_page 指令在conf中向上移动到调用return 404之前。

这应该有效:

# The default server.
#
server {
    listen       80 default_server;
    server_name  everythingelse;
    error_page 404 /error_docs/404.html;

    # Everything is a 404
    location / {
        return 404;
    }

    # Custom Error Page
    location /error_docs {
        alias /opt/local/html/;
        log_not_found  off;
        access_log off;
    }
}

这将为所有站点(服务器)使用相同的自定义站点。您需要添加错误文档位置。

http {
error_page 404 /error_docs/404.html;

...

    # The default server.
    #
    server {
        listen       80 default_server;
        server_name  everythingelse;

        # Everything is a 404
        location / {
            return 404;
        }

        # Custom Error Page
        location /error_docs {
            alias /opt/local/html/;
            log_not_found  off;
            access_log off;
        }
    }
}

2
投票

在 NGINX v1.14(2019-12-26 发布)中,您不能使用 location = /404.html。删除

=
(等号)有效:

server {
    listen 80;

    server_name everythingelse;
    error_page 404 /404.html;

    location / {
        return 404;
    }

    location /404.html {
        root /opt/local/html;
    }
}

0
投票

server
块之前,您可能正在使用
include
指令:

include /etc/nginx/conf.d/*.conf;
server {
    listen 80;
    ...
}

我遇到了同样的问题,并通过删除

include
行修复了它:

server {
    listen 80;
    ...
}

0
投票

由于

root
error_page
都是 http 块范围内的有效指令,因此可以利用 nginx 配置的继承行为。

要在我的所有虚拟主机之间共享自定义错误页面(以便对未知虚拟主机或已知虚拟主机中不存在的资源的请求根据 error_page 定义获取我的自定义错误页面作为响应),我使用以下配方。

1. 将这三行添加到

/etc/nginx/nginx.conf

# …
root /var/www/whatever # Used by undefined hosts
error_page 403 404 =404 /404.html
error_page 502 503 504 =500 /500.html
# …

2. 使用以下代码片段创建

/etc/nginx/sites-available/catchall
作为“catch all”默认虚拟服务器。

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    # SSL rules here if required
    server_name _;
}

3. 在必须使用自定义错误的每个文档根目录中创建 404.html 和 500.html 文件(或链接 /var/www/whatever 中的文件),否则将使用默认值。

也就是说,并非所有指令都可以从更高级别的范围继承,更糟糕的是,某些继承是不直观的并且行为不符合您的预期。

(使用

Debian9
nginx/1.10.3


0
投票
   location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #If the file isn't found, it generates a URI for a 404 error.
                try_files $uri $uri/ =404;
                
        }
        error_page=404 /404.html;
        #If the URI matches "404", it sends the file "404.html".
        location =404 {
        try_files 404.html;
        }
© www.soinside.com 2019 - 2024. All rights reserved.