Disable nginx cache for JavaScript files

Ok, I almost give up on this, but how can I turn off caching from Nginx for JavaScript files? I am using a docker container with Nginx. When I change something in the JavaScript file now, I need a few reboots until a new file appears.

How do I know Nginx and not browser / docker?

Browser: I used curl on the command line to simulate a request and had the same problems. In addition, I use the CacheKiller plugin and the cache is disabled in Chrome Dev Tools.

Docker: when I connect to the bash container and use cat after changing the file, I immediately get the correct result.

I changed my nginx.conf to sites-enabled on this (which I found in another stackoverflow thread)

 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml|html|htm)$ { # clear all access_log directives for the current level access_log off; add_header Cache-Control no-cache; # set the Expires header to 31 December 2037 23:59:59 GMT, and the Cache-Control max-age to 10 years expires 1s; } 

However, after restoring the containers (and make sure it is in the container with cat ), it still does not work. This is the full .conf

 server { server_name app; root /var/www/app/web; # Redirect to blog location ~* ^/blog { proxy_set_header Accept-Encoding ""; sub_filter 'https://testproject.wordpress.com/' '/blog/'; sub_filter_once off; rewrite ^/blog/(.*) /$1 break; rewrite ^/blog / break; proxy_pass https://testproject.wordpress.com; } # Serve index.html only for exact root URL location / { try_files $uri /app_dev.php$is_args$args; } location ~ ^/(app|app_dev|config)\.php(/|$) { fastcgi_pass php-upstream; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; # Prevents URIs that include the front controller. This will 404: # http://domain.tld/app_dev.php/some-path # Remove the internal directive to allow URIs like this internal; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml|html|htm)$ { # clear all access_log directives for the current level access_log off; add_header Cache-Control no-cache; # set the Expires header to 31 December 2037 23:59:59 GMT, and the Cache-Control max-age to 10 years expires 1s; } error_log /var/log/nginx/app_error.log; access_log /var/log/nginx/app_access.log; } 
+6
source share
5 answers

I have the following nginx virtual host (static content) for local development to disable all browser caching:

 server { listen 8080; server_name localhost; location / { root /your/site/public; index index.html; # kill cache add_header Last-Modified $date_gmt; add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; if_modified_since off; expires off; etag off; } } 

No cache headers sent:

 $ curl -I http://localhost:8080 HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Mon, 24 Jul 2017 16:19:30 GMT Content-Type: text/html Content-Length: 2076 Connection: keep-alive Last-Modified: Monday, 24-Jul-2017 16:19:30 GMT Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0 Accept-Ranges: bytes 

Last-Modified always the current time.

+13
source

The expires and add_header do not affect the caching of NGINX files, this applies only to what the browser sees.

Instead, you want:

 location stuffyoudontwanttocache { # don't cache it proxy_no_cache 1; # even if cached, don't try to use it proxy_cache_bypass 1; } 

Although usually .js, etc. is this what you will cache, maybe you just need to completely disable caching?

+3
source

What you are looking for is a simple directive, for example:

 location ~* \.(?:manifest|appcache|html?|xml|json)$ { expires -1; } 

The above will not cache extensions inside (). You can configure different directives for different types of files.

+2
source

Remember that set sendfile off; or cache headers do not work. I use this:

 location / { index index.php index.html index.htm; try_files $uri $uri/ =404; #.s. el /index.html para html5Mode de angular #.s. kill cache. use in dev sendfile off; add_header Last-Modified $date_gmt; add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; if_modified_since off; expires off; etag off; proxy_no_cache 1; proxy_cache_bypass 1; } 
0
source

I have the following Nginx virtual host (static content) for local development to disable all browser caching:

  upstream testCom { server localhost:1338; } server { listen 80; server_name <your ip or domain>; location / { # proxy_cache datacache; proxy_cache_key $scheme$host$request_method$request_uri; proxy_cache_valid 200 60m; proxy_cache_min_uses 1; proxy_cache_use_stale updating; proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Scheme $scheme; proxy_ignore_headers Set-Cookie; userid on; userid_name __uid; userid_domain <your ip or domain>; userid_path /; userid_expires max; userid_p3p 'policyref="/w3c/p3p.xml", CP="CUR ADM OUR NOR STA NID"'; add_header Last-Modified $date_gmt; add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; if_modified_since off; expires off; etag off; proxy_pass http://testCom; } } 
0
source

Source: https://habr.com/ru/post/1011690/


All Articles