简体   繁体   English

如何配置Nginx错误页面跳转到其他网站?

[英]How to configure Nginx error pages to jump to another website?

I configured a Nginx reverse proxy server for my website.我为我的网站配置了 Nginx 反向代理服务器。 I don't want users to see either 404 or 500 error pages, so I try to configure the.conf to jump to my website if the error pages are triggered.我不希望用户看到 404 或 500 错误页面,因此我尝试将.conf 配置为在触发错误页面时跳转到我的网站。 I searched for many solutions but am not sure if my configuration works because I don't know how to test it.我搜索了许多解决方案,但不确定我的配置是否有效,因为我不知道如何测试它。 Shamed.羞愧。 Any pro can take a look at my configuration and advise?任何专业人士都可以看看我的配置并提出建议吗?

I can not figure out how to use "location" for Nginx configuration exactly.我不知道如何准确地为 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 {
    }

In addition, I want the error_pages to also trigger another HTML page at the same time so that I can add a php email notification in the HTML. In addition, I want the error_pages to also trigger another HTML page at the same time so that I can add a php email notification in the HTML. That means when an error page is triggered, I can receive an email.这意味着当触发错误页面时,我可以收到 email。 How to configure the Nginx?如何配置 Nginx?

Many thanks.非常感谢。 Leo狮子座

All errors can trigger the PHP email notification script, and that PHP page would do the redirection.所有错误都可能触发 PHP email 通知脚本,并且 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 can be used to test and inspect the Location redirection header from the command line: Curl 可用于从命令行测试和检查Location重定向 header:

$ curl -I https://example.com/test-error-page

HTTP/2 302 
location: https://mywebsite.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM