Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Bad Gateway Page with Nginx

Tags:

nginx

Is it possible to serve a custom "Bad Gateway" error page in Nginx?

Similar to having custom 404 pages.

like image 511
deb Avatar asked Oct 17 '11 15:10

deb


People also ask

How do I create a custom 404 page in nginx?

Create a configuration file called custom-error-page. conf under /etc/nginx/snippets/ as shown. This configuration causes an internal redirect to the URI/error-page. html every time NGINX encounters any of the specified HTTP errors 404, 403, 500, and 503.

Where is nginx setup?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .


3 Answers

There are three pieces that must be in place in order for your custom error page to display instead of the generic "Bad Gateway" error.

  1. You must create an html file named something like "500.html" and place it in the root. In the case of Rails running behind Nginx, this means putting it at public/500.html.

  2. You must have a line in your config file that points at least the 502 errors to that 500.html page like this:

    error_page 502 /500.html;
    
  3. You must have a location block for /500.html in your config file. If your root is already defined, this block can be empty. But the block must exist nonetheless.

    location /500.html{
    }
    
like image 194
Jack Desert Avatar answered Oct 16 '22 18:10

Jack Desert


It's similar to setting up the custom 404 pages. Here's what I've got.

#site-wide error pages
error_page 404 /404.html;
error_page 500 502 503 504 /500.html;
like image 45
Larsenal Avatar answered Oct 16 '22 20:10

Larsenal


using debian (9.3 stretch actually) i did following steps:

  • create /var/www/html/502.html with the content for the 502 error page

  • edit /etc/nginx/sites-enabled/mywebsite.conf

so it looks similar like this:

server {
    listen 80; listen [::]:80;
    server_name www.mywebsite.com;

    error_page 502 /502.html;
    location /502.html {
        root /var/www/html;
    }
}
  • then restarted nginx using service nginx restart
like image 17
Christoph Lösch Avatar answered Oct 16 '22 18:10

Christoph Lösch