Nginx – Expire 410 responses in nginx

cachehttp-headershttp-status-codenginx

Browsers cache 410 Gone responses indefinitely by default. An accidental 410 can kill the URL forever. The URL might be also resurrected later for other reasons. I would like to set expires for 410s to force browser refresh time to time. Is there a way to do it with nginx?

server {
    # ...
    error_page 410 /errors/410.html;
    location /errors/ {
        internal;
        expires 1h;
    }
    location = /some/file {
        expires 1h;
        return 410;
    }
}

The above config results in responses without any cache control directives.

Best Answer

HTTP 410, according to Wikipedia, means

410 Gone - Indicates that the resource requested is no longer available and will not be available again.

You should use a more suitable response code. 444 "connection closed without response" or 404 "not found" might be more suitable, but this is something for you to look into.

I don't tend to use expires much in Nginx, I add headers. This gives me more control, being able to specify things like s-maxage, which controls shared cache max age. That requires the headers_more module to be compiled in, which it is by default for some platforms.

add_header Cache-Control "public, max-age=691200, s-maxage=691200";