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/javascript application/x-javascript

    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

            # 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


  • 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? – Tero Kilkanen Feb 8 '15 at 18:01
  • Actually, after I add location..., the browser get the Welcome page of Nginx when I go to the url. – Alexis Benoist Feb 10 '15 at 20:45
  • Try moving the root directive away from the location block, to the server level. – Tero Kilkanen Feb 10 '15 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.