如何配置 Nginx 错误页面跳转到另一个网站?
Posted
技术标签:
【中文标题】如何配置 Nginx 错误页面跳转到另一个网站?【英文标题】:How to configure Nginx error pages to jump to another website? 【发布时间】:2022-01-22 06:50:23 【问题描述】:我为我的网站配置了 nginx 反向代理服务器。我不希望用户看到 404 或 500 错误页面,因此我尝试将 .conf 配置为在触发错误页面时跳转到我的网站。我搜索了许多解决方案,但不确定我的配置是否有效,因为我不知道如何测试它。羞愧。有没有高手可以看看我的配置并给出建议?
我不知道如何准确地使用“位置”进行 Nginx 配置。
....
fastcgi_intercept_errors on;
....
error_page 404 http://mywebsite.com;
location = http://mywebsite.com
error_page 500 502 503 504 https://mywebsite.com;
location = https://mywebsite.com
此外,我希望 error_pages 也同时触发另一个 html 页面,以便我可以在 HTML 中添加 php 电子邮件通知。这意味着当触发错误页面时,我可以收到一封电子邮件。如何配置 Nginx?
非常感谢。 狮子座
【问题讨论】:
【参考方案1】:所有错误都可以触发 PHP 电子邮件通知脚本,并且该 PHP 页面会进行重定向。
server
error_page 500 /error.php;
location /test-error-page
return 500;
...
<?php
// sendEmailNotification(...)
// redirect to new page
header("Location: https://mywebsite.com", true, 302);
Curl 可用于从命令行测试和检查Location
重定向标头:
$ curl -I https://example.com/test-error-page
HTTP/2 302
location: https://mywebsite.com
【讨论】:
非常感谢,克里斯。以上是关于如何配置 Nginx 错误页面跳转到另一个网站?的主要内容,如果未能解决你的问题,请参考以下文章
Nginx配置web网站维护页面--->访问任意页面都要跳转到维护页面 #yyds干货盘点#