Search code examples
cachingnginxgzipsystem-administrationserver-administration

Nginx fails to load static files after I declared them using location


I've configured Nginx as you can see:

server {

    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/nginx/ssl/bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/privateKey.key;

    location /webmin/ {
            proxy_pass http://127.0.0.1:10000;
    }

server {

    listen 80;
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/privateKey.key;
    server_name localjob.it;
    access_log off;

    location / {
          alias /webapps/sitoweb/;
    }

Now if I go on mysite.com the page is loaded with the CSS, but if I add:

    location ~* \.(css|js|gif|jpe?g|png)$ {
          expires 168h;
          add_header Pragma public;
          add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

Now if I go on mysite.com the page can't load CSS. I can't understand the reason!!


Solution

  • Nginx locations exclusive so your alias inslide root location doesn't applies to another locations. Also it's a bit misuse, just use root directive in server block.

    server {
        listen 80;
        listen 443 ssl;
        ssl_certificate /etc/nginx/ssl/bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/privateKey.key;
        server_name localjob.it;
        access_log off;
    
        root /webapps/sitoweb;
    
        location ~* \.(css|js|gif|jpe?g|png)$ {
              expires 168h;
              add_header Pragma public;
              add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }