0

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.

1 Answers1

1

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";
Tim
  • 31,888
  • 7
  • 52
  • 78
  • I tested this now. Replacing `expires` with `add_header` doesn't change response headers. – Robert Važan May 17 '17 at 20:28
  • 444 a 302 are hopelessly unsuitable error codes. I can choose between 404 and 410. 410 is more semantic and it allows me to differentiate removed URLs from broken links in access logs. – Robert Važan May 17 '17 at 20:30
  • The main point of my answer was 410 "gone" was inappropriate for content that may come back in future. Use whatever response code you think is best. 404 seems fine. – Tim May 17 '17 at 20:32
  • If you want to get past browsers' cache of 410 HTTP status code, then you can add a query string to the end of URL, like `http://www.example.com/410url?asdf`. You would have to update the URL on all of your pages. – Tero Kilkanen May 18 '17 at 01:22
  • I am using 410s for URLs that are really permanently gone, but they can be repurposed in the future (for different content) and start returning 200s again. It is also possible that 410 makes its way to `location /` by accident and I want to limit damage in that case. – Robert Važan May 18 '17 at 08:56