Trashpanda
Trashpanda

Reputation: 14798

How to return no body / content for errors / error_page in nginx?

If a file is not found, or forbidden, etc. -- I'd really like to just return the HTTP status code and no body content. Something like:

error_page 403 404 body="";

What are the current (2015) best practices?

Upvotes: 3

Views: 5675

Answers (3)

wick
wick

Reputation: 2224

Realistically, all current options imply location per error code wich isn't a clean solution. When you compile your nginx, simply deal with bodies in src/http/ngx_http_special_response.c

Upvotes: -2

Aleks
Aleks

Reputation: 71

I am not sure if this is the best practise but it still works even without creating actual empty file.

error_page 403 404 /__empty-error-page__.html;

location = /__empty-error-page__.html {
  internal;
  return 200 "";
}

Upvotes: 7

Trashpanda
Trashpanda

Reputation: 14798

The best I've come up with is to create a zero-byte file:

$ touch _.json

set that location to be internal in my nginx config, and point the error_page to it:

location /_.json { internal; } error_page 403 404 /_.json;

Depending on your content-type, replace _.json with your desired _.mime-type-extension.

Upvotes: 0

Related Questions