我正在运行 nginx 服务器。我只想为特定请求提供自定义错误页面。例如请求
http://localhost/abc1 & http://localhost/abc2
如果这些页面不存在,我想提供一个自定义错误页面。此自定义错误页面应仅针对上述两个链接显示,其余页面错误可以显示默认错误页面。我尝试了不同的配置,但似乎没有任何效果。想法
问候, Farrukh Arshad。
我正在运行 nginx 服务器。我只想为特定请求提供自定义错误页面。例如请求
http://localhost/abc1 & http://localhost/abc2
如果这些页面不存在,我想提供一个自定义错误页面。此自定义错误页面应仅针对上述两个链接显示,其余页面错误可以显示默认错误页面。我尝试了不同的配置,但似乎没有任何效果。想法
问候, Farrukh Arshad。
您的回答非常正确,但正如您所说,您只为 html 定义了它们,删除扩展名,它应该适用于整个目录,无需重复根目录,只需在服务器块范围内定义它
server {
listen 80;
root /var/www/nginx-default;
location /abc1 {
error_page 404 /special_error.html;
}
location /abc2 {
error_page 404 /special_error2.html;
}
}
好的,我找到了答案。诀窍是您必须为所有这些特殊位置显式定义 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;
}
我不擅长 nginx,但我注意到这取决于您在“位置”标签中给出的搜索模式。我尝试了不同的东西,但都失败了。例如,上述规则仅适用于
http://localhost/abc1.html
并失败
http://localhost/abc1
因此,如果您想涵盖第二种情况,您的“位置”搜索模式应该很好。可能一些 nginx 大师可以对此有所了解。谢谢。