如何将异常处理程序配置全局添加到 /etc/nginx/sites-available 下的服务器块?

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

我想应用 403 禁止默认错误的解决方案。我在 /etc/nginx/sites-available 下有 100 个站点。我创建了一个 403.html 文件(/usr/share/nginx/html),并在 */etc/nginx/snippets/custom_exception.conf 中添加了以下代码 *

    error_page 403 /403.html;

    location = /403.html {
       root /usr/share/nginx/html;
    }

我通过添加以下行测试了一个站点的代码

include /etc/nginx/snippets/custom_exception.conf;

工作正常。代码应该位于每个文件的服务器块中。问题是我不相信要对生产服务器中的每个文件进行此更改。

我在 nginx.conf 文件中尝试了几种方法,如下所示,但不起作用

http {
...
 
 include /etc/nginx/snippets/custom_exception.conf;
 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;

}
http {
...
 server {
    include /etc/nginx/snippets/custom_exception.conf;
 }

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;

}
nginx nginx-config nginx-location
1个回答
0
投票

error_page
指令可以放置在
http
上下文中。

不幸的是,

location
指令(或包含它的包含文件)必须复制到每个相关的
server
块中。

至少,如果您编辑每个

server
块以添加
include
语句 - 您只需要执行一次。


或者,如果这对您和您的客户有效,您可以将 403.html 页面放入单独的域中,并在

error_page
语句中指定完整的 URL。这消除了编辑每个
server
块的要求。

例如:

error_page 403      http://example.com/forbidden.html;
© www.soinside.com 2019 - 2024. All rights reserved.