Toseef Zafar
Toseef Zafar

Reputation: 1767

nginx - enabling cache for .css file with gzip compression

I have following nginx config. if I remove the cache config for css everything works and all css files load perfectly via reverse proxy. but when I put in cache config for .css that results in 404 for all my css resources:

location ~* \.css {
          add_header Cache-Control public;
          add_header Pragma public;
          add_header Vary Accept-Encoding;
          expires 1M;
        } 

location / {
            proxy_pass http://localhost:8080;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header X-Forwarded-Port $server_port;
        }

Upvotes: 1

Views: 544

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

Nginx chooses a single location to process a request. That location needs to be complete. See how Nginx processes a request.

Your location ~* \.css block is missing the proxy_pass statement.

The proxy_set_header statements can be moved to the outer block and inherited by both location blocks.

For example:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;

location / {
    proxy_pass http://localhost:8080;
}

location ~* \.css {
    add_header Cache-Control public;
    add_header Pragma public;
    add_header Vary Accept-Encoding;
    expires 1M;

    proxy_pass http://localhost:8080;
}

Upvotes: 2

Related Questions