Enable nginx browser caching for specific url

5,676

Solution 1

From the nginx wiki it seems you have a space in between "/static" and your regex pattern. Location takes only one "uri" argument, not two...and nginx will interpret that space as an argument delimiter.

Also you need to tell a location block you want to match a regular expression with the '~' or '~*' modifiers.

You might try this

location ~* "/static.*(jpg|jpeg|png|gif|ico|css|js)$" {

instead of what you have and see if that does what you want.

Solution 2

Do you have any other files in this folder? I you don't or if it's ok to have them exposed you could simplify config:

server {
        listen  80;
        listen  443 ssl;
        server_name     www.example.com example.com;
        ssl_certificate /var/www/example/ssl/ssl.crt;
        ssl_certificate_key     /var/www/example/ssl/ssl.key;
        root /var/www/example;

        location / {
                proxy_pass      http://127.0.0.1:8000;
        }

        location /media/ {
                # it's ok to have empty location
        }

        location /static/ {
                expires 3d;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
}
Share:
5,676

Related videos on Youtube

Áurea MT Dias
Author by

Áurea MT Dias

Developer interested in programming with Go, Python, Javascript, and C

Updated on September 18, 2022

Comments

  • Áurea MT Dias
    Áurea MT Dias almost 2 years

    How can I enable nginx cache for specific URL? I have a django web app and I put my static files such as CSS, JavaScript and ... in a folder called static and I can access them via http://example.com/static/ and I want to config nginx n order to catch these files only from this url. I've read http://www.nginxtips.com/how-to-enable-browser-cache-static-files-on-nginx/ but this option wasn't explained there. I tried below config and restarted my nginx server but I've got errors. This is my sites-available/default config file. What's the problem?

    server {
            listen  80;
            listen  443 ssl;
            server_name     www.example.com example.com;
            ssl_certificate /var/www/example/ssl/ssl.crt;
            ssl_certificate_key     /var/www/example/ssl/ssl.key;
            location /static (jpg|jpeg|png|gif|ico|css|js)$ {
                    alias   /var/www/example/static;
                    expires 3d;
                    add_header Pragma public;
                    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    
            }
            location /media  {
                    alias  /var/www/example/media;
            }
            location / {
                    proxy_pass      http://127.0.0.1:8000;
            }
    }
    

    And this is nginx log file content...

    2014/03/07 13:02:28 [error] 25189#0: *22 open() "/etc/nginx/html/static/css/wait.css" failed (2: No such file or directory), client: 2.178.27.157, server: www.example.com, request: "GET /static/css/wait.css HTTP/1.1", host: "example.com", referrer: "http://example.com/"
    2014/03/07 13:02:28 [error] 25189#0: *23 open() "/etc/nginx/html/static/img/infographi.png" failed (2: No such file or directory), client: 2.178.27.157, server: www.example.com, request: "GET /static/img/infographi.png HTTP/1.1", host: "example.com", referrer: "http://example.com/"
    2014/03/07 13:02:29 [error] 25189#0: *23 open() "/etc/nginx/html/favicon.ico" failed (2: No such file or directory), client: 2.178.27.157, server: www.example.com, request: "GET /favicon.ico HTTP/1.1", host: "example.com"
    2014/03/07 13:08:41 [emerg] 25220#0: invalid location modifier "/static" in /etc/nginx/sites-enabled/default:8
    2014/03/07 13:08:41 [emerg] 25221#0: invalid location modifier "/static" in /etc/nginx/sites-enabled/default:8
    2014/03/07 13:09:50 [emerg] 25255#0: invalid location modifier "/static" in /etc/nginx/sites-enabled/default:8
    2014/03/07 13:09:50 [emerg] 25256#0: invalid location modifier "/static" in /etc/nginx/sites-enabled/default:8