0

I'm trying to make cache work on a Nginx which is a proxy for gunicorn or which serves directly static files.

I tried to add:

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        root /home/admin/kids/app/static;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

But when I do that the server stops working.

server {
    listen 80;
    server_name example.com;
    gzip_static on;
    gzip on;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_length 500;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/html text/xml text/css
               text/comma-separated-values
               text/javascript application/x-javascript
               application/atom+xml;

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        root /home/admin/kids/app/static;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    # Handle all locations
    location / {
            try_files $uri @proxy_to_app;
    }
    location @proxy_to_app {
            # Pass the request to Gunicorn
            proxy_pass http://127.0.0.1:8000;

            # Set some HTTP headers so that our app knows where the
            # request really came from
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

There was the same problem here but I don't understand the comment which helps out.

Probably another block had definition for the static files with a root set, in that case you should add the directives to that block. (I know this is 2y late, but for future citizens) – aularon

Thanks.

3
  • 2
    What is the line with example.com? That is not a valid configuration line. What exactly happens when "server stops working"? What are the relevant nginx error.log and access.log lines? And what is the complete nginx configuration? Feb 8, 2015 at 18:01
  • Actually, after I add location..., the browser get the Welcome page of Nginx when I go to the url. Feb 10, 2015 at 20:45
  • Try moving the root directive away from the location block, to the server level. Feb 10, 2015 at 22:14

0

You must log in to answer this question.

Browse other questions tagged .