Home > Software engineering >  How to configure Nginx error pages to jump to another website?
How to configure Nginx error pages to jump to another website?

Time:12-24

I configured a Nginx reverse proxy server for my website. 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. 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.

    ....
    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. That means when an error page is triggered, I can receive an email. How to configure the Nginx?

Many thanks. Leo

CodePudding user response:

All errors can trigger the PHP email notification script, and that PHP page would do the redirection.

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 -I https://example.com/test-error-page

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