nginx:如何在每个服务器块中全局提供错误页面?

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

我有 3 个域:example.com、a.example.com 和 b.example.com,它们都有自己的服务器块。 我想在所有域/子域的路径

/var/www/errors/404.html
上提供 404 错误。

目前,似乎唯一的解决方法是手动将

location
块粘贴到所有
server
块上,但我觉得好像必须有更好的方法来做到这一点。

我试过的:

server { 
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
 
        server_name _;
 
        # Add index.php to the list if you are using PHP
        index index.html index.htm;
        
        error_page 404 /404.html;
 
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        
        location ~ /\.(?!well-known).* {
                deny all;
        }
 
        location = /404.html {
                root /var/www/errors;
        }
 
        merge_slashes off;
        rewrite ^(.*?)//+(.*?)$ $1/$2 permanent;
}

结果是在无效服务器(例如 c.example.com)上提供了正确的页面,但在 example.com、a.example.com 和 b.example.com 上都没有提供。

nginx installation webserver hosting nginx-config
© www.soinside.com 2019 - 2024. All rights reserved.