Friday, 12 May 2017

Nginx Static File Cache giving 404s

I've tried a ton of different stuff to make it work and looked at a bunch of previous Nginx configuration guides, but my site keeps giving me 404 not founds for all of my static content. Below is my configuration.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
   server_name mywebsite.com www.mywebsite.com;
    return 301 https://$server_name$request_uri;
}
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";

server {
        access_log off;
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-mywebsite.com.conf;
        include snippets/ssl-params.conf;

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                expires 30d;
                add_header Vary Accept-Encoding;
                access_log off;
        }

        # Pass requests for / to localhost:3000:
        location / {
                proxy_cache my_zone;
                proxy_cache_bypass  $http_cache_control;
                add_header X-Proxy-Cache $upstream_cache_status;
                include proxy_params;

                proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
                proxy_ignore_headers Set-Cookie;
                proxy_hide_header Set-Cookie;
                proxy_hide_header X-powered-by;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:3000;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }
}

For more background, my static files are all stored in a public folder, so like: /www/Project/public/css/styles.css or /www/Project/public/images/myimage.jpg

The site works if I take away the location ~* .(?:ico|css|js|gif ... ) but then it's not caching.

Does anyone have an idea as to what's going on?



via BZhou

No comments:

Post a Comment