I'm looking for a way to serve up a static message from nginx if an upstreams section returns certain HTTP codes. I understand that I could use static pages and ask NGINX to serve these up, but this is slightly more complicated that I need - all really need is (in pseudocode) to say

if($code_from_proxy_upstream == 500){
   return 500 'Error Occured'

I Read this question and this question, but I don't think that they quite do what I need. I think that I need some combination of the two, but not quite sure of the syntax.

  • 1
    Not really. You can either have nginx pass through the errors from the backend, or serve a static document. – Michael Hampton Sep 1 '15 at 21:19
  • Your example is confusing. – Alexey Ten Sep 2 '15 at 5:03

I pretty much got this to work.

I added a set of directives like this:

error_page 508 @myerrordirective_508;

and then

location @myerrordirective_508{
   return 508 "Error Occured"

The only pain was that nginx doesn't seem to allow me to use a variable to refer to the status (so that I could have just had one block with $status or whatever in it, which meant that each status needed an error_page and a location block.I therefore used a script to generate lines like the above for each error code, and put these into an include file which I linked from the main config

Seemed to do what I wanted.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.