Nginx: How to Use Error_Page

Error_page is touched, and it’s logged here
1. Error_page syntax
Grammar:

error_page code [ code... ] [ = | =answer-code ] uri | @named_location 

Default value:

no 

Use fields:
HTTP, Server, location, if field in location
Example 2.
The nginx directive error_page is designed to display a predefined URI when an error occurs, such as:

error_page 502 503 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}   

This actually creates an internal redirect, which returns the contents of 50x.html when the visit appears 502 or 503. Notice if you can find the 50x.html page, so add a location to make sure you find your custom 50x page.
Can we also define the return state in this case by ourselves, such as:

error_page 502 503 =200 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}   

In this way, when the user accesses 502 and 503, the return status of the user is 200 and the content is 50x.html.
When the error_page is followed by something other than a static message, such as proxyed Server or FastCGI/ UWSGI /SCGI Server, the status returned by the server (200, 302, 401, or 404) can also be returned to the user.

error_page 404 = /404.php;
location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}   

You can also set a named location and do the corresponding processing inside.

error_page 500 502 503 504 @jump_to_error;
location @jump_to_error {    
    proxy_pass http://backend;
}

It also handles error pages by having clients redirect 302, 301, etc. The default status code is 302.

error_page 403      http://example.com/forbidden.html;
error_page 404 =301 http://example.com/notfound.html;

Meanwhile, error_page can only respond once in a request, and the corresponding nginx has another configuration that controls this option: recursive_error_pages
default to false, which controls whether error_page can trigger multiple times in a request.
2. Nginx custom 404 error page configuration has no difference between equals sign
Error_page 404 /404. HTML displays custom 404 page content and normally returns a 404 status code. Error_page 404 = /404.html displays custom 404 page content but returns a 200 status code. Error_page 404 /404. PHP if it is a dynamic 404 error page containing header code (such as a 301 jump), it will not execute properly. Returns the 404 code normally. Error_page 404 = /404.php If it is a dynamic 404 error page that contains header code (such as a 301 jump), the equal sign configuration executes normally, and returns a status code defined in PHP. However, if the PHP definition returns a 404 status code, the 404 status code can be returned normally, but the custom page content cannot be displayed (the default 404 page appears), in this case, consider using the 410 code instead (header(“HTTP/1.1 410 Gone”); Normal return 410 status code, and can normally display custom content).

example

server  {
    listen 80;
    server_name  test.com;
    index       index.html index.htm;

    location/{ 
        proxy_pass http://online;
        error_page 404 = @fallback;
        proxy_intercept_errors on;
    }
    location @fallback {
        proxy_pass http://backend;
    }
}

upstream online {
         server 192.168.88.18:80;
         server 192.168.88.28:80;
}

upstream backend {
         server 192.168.88.38:80;
}

example
Limit_req traffic limit is set in the nginx configuration, so many requests return 503 error Code. For the purpose of improving the user experience, we want to return normal Code 200 and frequent operation information:

location  /test {
  ... 
  limit_req zone=zone_ip_rm burst=1 nodelay; 
  error_page 503 =200 /dealwith_503?callback=$arg_callback;
}
location /dealwith_503{ 
  set $ret_body '{"code": "V00006","msg": "Operate too often, please sit down and have a cup of tea."}';
   if ( $arg_callback != "" ) 
   { 
       return 200 'try{$arg_callback($ret_body)}catch(e){}'; 
   } 
   return 200 $ret_body; 
}


Read More: