Nginx – Configure NGINX : How to handle 500 Error on upstream itself, While Nginx handle other 5xx errors

error handlingnginx

We have a NGINX reverse proxy (clustered) with 45 upstreams (22 domains, 20 subdomains, 11 apps).

Some of our projects hosts apis for some users globally.

Our developers designed special custom 500 responses for special cases and want to show that messages only for tomcat_api upstream.

They want to serve 500 pages and handle exceptions on tomcat_api upstream members not on NGINX. But we want to handle all other 5xx errors on NGINX.

As I can not succeeded need a hand right now.

Here is the server configuration follows, could you please help?

server { 
  server_name api.hotcoldwarm.com;

  location / {
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header x-redirect-uri hotcoldwarm;
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; always";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://tomcat_api;
    proxy_next_upstream error timeout http_502 http_503 http_504 http_404;
    proxy_connect_timeout   1;
  }
    listen 80;
    listen 443 ssl;
    ssl_certificate /etc/rapidssl/live/api.hotcoldwarm.com/fullchain.pem;
    ssl_certificate_key /etc/rapidssl/live/api.hotcoldwarm.com/privkey.pem;
    include /etc/rapidssl/options-ssl-nginx.conf;
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }

}

Thank you.

Best Answer

You probably want to consider proxy_intercept_errors on; option. Then configure custom error_page after it. See answer https://stackoverflow.com/a/8715597/434255