Nginx: TLS Termination for RESTful Service Responding in JSON
This is quite common nowadays to have some TLS terminatin reverse proxy in front of your REST API. Nginx is being used frequently for this purpose.
The problem I faced and spent some time trying to resolve is that API backing Nginx responds in JSON, but if something is wrong with the request itself or Nginx can’t reach the backend Nginx returns error page in ‘text/html’.
Basically what I was trying to do is to make nginx respond ‘application/json’ on 502 errors, when no backends is reachable and on 400 errors, when someone tries to get plain HTTP response on 443 port. Other errors are expected to be reported by API itself.
502s were easy, you just add error_page 502 directive and location to server definition and got what you want.