Nginx Configuration – Fix Nginx 302 Redirect to File Not Updating

cacheconfigurationnginx

We have a Debian 9.4 box running nginx 1.10.3. The purpose of the box is to provide redirects to files and resources so that Marketing can provide links in their materials, where the underlying content can be pointed at another asset if the document is updated. That way, customers are always accessing the correct version for reference.

Even though we set it up to be as basic as possible, we are seeing odd behavior that looks like caching. We can change what the redirect points to, but it continues to resolve to the old version.

For reference, here's our site config:

server {
    listen 80;

    server_name files.oursite.com;

    index index.html;

    error_page 404 http://files.oursite.com/error.html;

    location / {
      try_files $uri $redirect;
    }

    location @redirect {
      include /home/fileserve/redirect.conf;
    }

Inside redirect.conf, this would appear:

rewrite /foo http://files.oursite.com/foo.pdf redirect;

That'll work fine. but if we update it to this:

rewrite /foo http://files.oursite.com/bar.pdf redirect;

foo.pdf still pops up.

The header response from nginx shows it is still serving foo.pdf, and I've tried it from multiple machines, browsers, and network connections to verify it's not caching under any local circumstances. This is also after I have switched to 302s from 301s, because we do NOT want local caching.

Does anyone know what is happening, and how to resolve this? Thanks!

Edit: Adding troubleshooting as requested:

nginx -t syntax ok and test was successful.
nginx -T shows the redirect loading (/foo should redirect to /bar.pdf)
curl as follows:

curl -i http://files.oursite.com/foo:

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Fri, 25 Jan 2019 18:18:36 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://files.oursite.com/bar.pdf

curl -I output directly to the files, per @Richard Smith:

for the old file (foo.pdf):

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 25 Jan 2019 18:39:12 GMT
Content-Type: application/pdf
Content-Length: 980444
Last-Modified: Tue, 20 Nov 2018 18:10:25 GMT
Connection: keep-alive
ETag: "5bf44e11-ef5dc"
Accept-Ranges: bytes

for the new file (bar.pdf):

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 25 Jan 2019 18:39:45 GMT
Content-Type: application/pdf
Content-Length: 5948805
Last-Modified: Thu, 24 Jan 2019 23:23:50 GMT
Connection: keep-alive
ETag: "5c4a4906-5ac585"
Accept-Ranges: bytes

Curl -I after adding expires -1;

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Fri, 25 Jan 2019 20:42:59 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://files.oursite.com/foo.pdf
Expires: Fri, 25 Jan 2019 20:42:58 GMT
Cache-Control: no-cache

The most difficult part in all of this is remembering to wipe my URLs 🙂

Best Answer

I think I figured out the answer, but someone may want to add clarity as to why this is happening.

My redirect file actually has the following:

rewrite /foo http://files.oursite.com/foo.pdf redirect;
rewrite /foo-EXAMPLE http://files.oursite.com/foo-EXAMPLE.pdf redirect;

For whatever reason, typing /foo-EXAMPLE always picks up the redirect for /foo. This would explain why no matter the changes I make to /foo-EXAMPLE, it picks up /foo.pdf. If I delete /foo from the redirect file, /foo-EXAMPLE works correctly.

It also seems that the -EXAMPLE can be prepended to any url, and it'll still grab /foo. I have no idea why.