Difference between revisions of "Serve custom error pages from reverse proxy"

From Wiki
Jump to: navigation, search
(Created page with "Nginx: Serve error pages from reverse proxy, not from upstream server If you want nginx to process errors directly you will need to add the following directive to the proxy c...")
 
 
Line 7: Line 7:
 
Without this, Nginx will forward the error page coming from the upstream server to the client.
 
Without this, Nginx will forward the error page coming from the upstream server to the client.
  
To customize the error pages returned location directives can be added.
+
To customize the error pages returned location directives can be added.  For example.
  
 
<pre>
 
<pre>
   error_page 400 /400.html;
+
   error_page 404 /custom_404.html;
  location /400.html {
+
    root  /var/www/errorpages;
+
    internal;
+
  }
+
  
  error_page 500 /500.html;
+
   location = /custom_404.html {
   location /500.html {
+
     root  /usr/share/nginx/html;
     root  /var/www/errorpages;
+
 
     internal;
 
     internal;
 
   }
 
   }
 +
</pre>
  
  error_page 502 /502.html;
+
This can be done for any error code. For example, a custom 500 error page is specified as follows.
  location /502.html {
+
    root  /var/www/errorpages;
+
    internal;
+
  }
+
  
   error_page 503 /503.html;
+
<pre>
  location /503.html {
+
   error_page 500 /500.html;
    root  /var/www/errorpages;
+
    internal;
+
  }
+
  
  error_page 504 /504.html;
+
   location /500.html {
   location /504.html {
+
 
     root  /var/www/errorpages;
 
     root  /var/www/errorpages;
 
     internal;
 
     internal;
 
   }
 
   }
 
</pre>
 
</pre>

Latest revision as of 16:35, 3 December 2018

Nginx: Serve error pages from reverse proxy, not from upstream server

If you want nginx to process errors directly you will need to add the following directive to the proxy config.

proxy_intercept_errors on;

Without this, Nginx will forward the error page coming from the upstream server to the client.

To customize the error pages returned location directives can be added. For example.

  error_page 404 /custom_404.html;

  location = /custom_404.html {
    root   /usr/share/nginx/html;
    internal;
  }

This can be done for any error code. For example, a custom 500 error page is specified as follows.

  error_page 500 /500.html;

  location /500.html {
    root   /var/www/errorpages;
    internal;
  }