1

I have an nginx.conf file currently that looks like this (with brackets replacing sensitive data):

worker_processes  3;

events {
    worker_connections  1024;
}

http {
    access_log  [/...];
    error_log   [/...]  crit;

    include mime.types;
    sendfile on;

    server {
        server_name [...] [...];
        return 301 [...] $request_uri;
    }

    server {
        listen 127.0.0.1:[...];
        root [/...];

        location / {
            include uwsgi_params;
            uwsgi_pass [.../uwsgi.sock];
        }
    }
}

If I add the following line after the existing location {...} clause, as suggested here, loading the website will produce "404 not found" errors for image, CSS, and js assets:

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1d;
}

How can I implement browser caching, without causing the "404" issue?

2 Answers 2

3

It's a simple issue of your location statements being out of order.

Your wrote that you put the static file location clause after the location / {} block. This means your static files will be checked for there, first. Since your uwsgi socket can't find the file paths, it returns 404.

What you want will look more like this.

http {
    [..]

    server {
        server_name example.com;
        return 301 ^ $request_uri;
    }

    server {
        listen localhost;
        root /path/to/webroot/;

        location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 1d;
        }
        location / {
            include uwsgi_params;
            uwsgi_pass [...];
        }
    }
}

The good people at Digital Ocean have an excellent guide explaining how this works. I recommend reading it through.

3
  • I had tried your recommendation, and it seemed to be working, so I marked the answer as correct. A few minutes later, I tried visiting the site again, and I'm getting the same 404 error. Not sure what's going on here.
    – Boa
    Mar 23, 2017 at 17:21
  • @Boa then it sounds like it's more likely an issue with your uwsgi implementation. Without knowing more about your setup, I can't really say.
    – Ryder
    Mar 23, 2017 at 18:01
  • Thanks. Can you offer any advice on what to look for, or on what sort of additional data I should post that could be useful to diagnosing the issue?
    – Boa
    Mar 23, 2017 at 18:08
0

The solution that resolved my issue was to put the uwsgi_pass [.../uwsgi.sock]; clause inside of each location {...} block.

1
  • Sure. Do you know why?
    – Ryder
    Mar 25, 2017 at 0:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .