Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Return common errors as json in Nginx
error_page 500 /500.html;
location /500.html{
return 500 '{"error": {"status_code": 500,"status": "Internal Server Error"}}';
}
error_page 502 /502.html;
location /502.html{
return 502 '{"error": {"status_code": 502,"status": "Bad Gateway"}}';
}
error_page 503 /503.html;
location /503.html{
return 503 '{"error": {"status_code": 503,"status": "Service Temporarily Unavailable"}}';
}
error_page 504 /504.html;
location /504.html{
return 504 '{"error": {"status_code": 504,"status": "Gateway Timeout"}}';
}
error_page 400 /400.html;
location /400.html{
return 400 '{"error": {"status_code": 400,"status": "Bad Request"}}';
}
error_page 401 /401.html;
location /401.html{
return 401 '{"error": {"status_code": 401,"status": "Unauthorized"}}';
}
error_page 403 /403.html;
location /403.html{
return 403 '{"error": {"status_code": 403,"status": "Forbidden"}}';
}
error_page 404 /404.html;
location /404.html{
return 404 '{"error": {"status_code": 404,"status": "Not Found"}}';
}
error_page 408 /408.html;
location /408.html{
return 408 '{"error": {"status_code": 408,"status": "Request Timeout}}';
}
error_page 418 /418.html;
location /418.html{
return 418 '{"error": {"status_code": 418,"status": "I\'m a teapot"}}';
}
@eriadam

This comment has been minimized.

Copy link

eriadam commented Jan 18, 2019

You can also add the proper Content-Type to the header:

  error_page 500 /500.html;
  location /500.html {
    more_set_headers 'Content-Type: application/json charset=UTF-8';
    return 500 '{"error": {"status_code": 500,"status": "Internal Server Error"}}';
  }
@adonese

This comment has been minimized.

Copy link

adonese commented Apr 27, 2019

@eriadam more_set_headers is not working. Use add_header instead.
the syntax will be different though.

add_header 'Content-Type' 'application/json charset=UTF-8';

More here

@vinayan3

This comment has been minimized.

Copy link

vinayan3 commented May 14, 2019

If you set the file name endings json then the correct Content-Type header is generated. You can see that in the latest Nginx mime type file, https://github.com/nginx/nginx/blob/master/conf/mime.types.

@fragsalat

This comment has been minimized.

Copy link

fragsalat commented Mar 16, 2020

Neither add_header nor more_set_headers worked for me.
I had to put default_type application/json; to tell nginx the content type is different from occet-stream

    location /this-is-now-an-error {
      default_type application/json;
      return 400 '{"error": "Because you get fired this is not allowed anymore"}';
    }
@wiseminds

This comment has been minimized.

Copy link

wiseminds commented Apr 27, 2020

for me i just changed .html to .json and it worked just fine.
for example

   error_page 404 /404.json;
    location /404.json{
    return 404 '{"error": {"status_code": 404,"status": "Not Found"}}';
    }
@JulienBreux

This comment has been minimized.

Copy link

JulienBreux commented Jul 8, 2020

My tiny contribution.

server {
    
    # your configuration here
    
    error_page 500 /500.json;
    location /500.json {
        return 500 '{"error":{"code":500,"message":"Internal Server Error"}}';
    }
    
    error_page 502 /502.json;
    location /502.json {
        return 502 '{"error":{"code":502,"message":"Bad Gateway"}}';
    }
    
    error_page 503 /503.json;
    location /503.json {
        return 503 '{"error":{"code":503,"message":"Service Temporarily Unavailable"}}';
    }
    
    error_page 504 /504.json;
    location /504.json {
        return 504 '{"error":{"code":504,"message":"Gateway Timeout"}}';
    }
    
    error_page 400 /400.json;
    location /400.json {
        return 400 '{"error":{"code":400,"message":"Bad Request"}}';
    }
    
    error_page 401 /401.json;
    location /401.json {
        return 401 '{"error":{"code":401,"message":"Unauthorized"}}';
    }
    
    error_page 403 /403.json;
    location /403.json {
        return 403 '{"error":{"code":403,"message": "Forbidden"}}';
    }
    
    error_page 404 /404.json;
    location /404.json {
        return 404 '{"error":{"code":404,"message":"Not Found"}}';
    }
    
    error_page 408 /408.json;
    location /408.json {
        return 408 '{"error":{"code":408,"message":"Request Timeout}}';
    }
    
    error_page 418 /418.json;
    location /418.json {
        return 418 '{"error":{"code":418,"message":"I\'m a teapot"}}';
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.