nginx中有多个404错误页面

Far*_*had 5 nginx

我正在运行nginx服务器.我想仅为特定请求提供自定义错误页面.例如请求

http://localhost/abc1 & http://localhost/abc2
Run Code Online (Sandbox Code Playgroud)

如果这些页面不存在,我想提供自定义错误页面.此自定义错误页面应仅针对上述两个链接出现,其余页面错误可显示默认错误页面.我尝试了不同的配置,但似乎没有任何工作.思考

此致,Farrukh Arshad.

Far*_*had 3

好的,我找到了答案。诀窍是您必须为所有这些特殊位置显式定义 error_page 。这是对我有用的配置。

location / {
    root   /var/www/nginx-default;
    index  index.html index.htm;
    error_page 404 /404.html;
}

location /abc1.html {
    root   /var/www/nginx-default;
    error_page 404 /special_error.html;
}
location /abc2.html {
    root   /var/www/nginx-default;
    error_page 404 /special_error2.html;
}
Run Code Online (Sandbox Code Playgroud)

我不擅长 nginx,但我注意到它取决于您在“location”标签中给出的搜索模式。我尝试过不同的事情,但都失败了。例如上述规则仅适用于

http://localhost/abc1.html 
Run Code Online (Sandbox Code Playgroud)

并失败

http://localhost/abc1 
Run Code Online (Sandbox Code Playgroud)

因此,如果您想涵盖第二种情况,您的“位置”搜索模式应该很好。也许一些 nginx 专家可以对此进行更多阐述。谢谢。