2016-10-25 216 views
6

好吧,我幾乎放棄了這一點,但是如何禁用Nginx緩存JavaScript文件?我在使用Nginx的Docker容器。當我現在改變JavaScript文件中的內容時,我需要多次重新加載,直到新文件在那裏。爲JavaScript文件禁用nginx緩存

我怎麼知道它是Nginx,而不是瀏覽器/ docker?

瀏覽器:我在命令行上使用curl來模擬請求併產生相同的問題。另外,我正在使用CacheKiller插件,並在Chrome開發工具中禁用緩存。

Docker:當我連接到容器的bash,並在更改文件後使用cat時,我立即得到正確的結果。

我爲sites-enabled這個(我在另一個計算器線程中)改變了我的nginx.conf

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; 
} 

然而,重建容器(並確保它在與cat容器)後,它仍然沒沒有工作。這下面是完整的.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; 
} 

回答

3

expiresadd_header指令對NGINX緩存文件沒有影響,這些都是純粹的關於瀏覽器看到的內容。

你可能要代替的是:

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

雖然通常的.js等是你能緩存的東西,所以也許你應該完全禁用緩存?

+0

沒關係,我在生產中進行緩存,只是不在開發中。我會嘗試你的建議 – Musterknabe

+0

陷阱,那麼有道理。 –

+0

我剛剛將你的代碼添加到我的'app.conf'中,但現在nginx甚至不再啓動。當我使用'nginx -t'時,出現錯誤'unknown directive'proxy_no_store「' – Musterknabe

2

你所尋找的是像一個簡單的指令:

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

以上將不會在()緩存的擴展。您可以爲不同的文件類型配置不同的指令。

12

我對當地的開發工作如下nginx的虛擬主機(靜態內容),禁止所有的瀏覽器緩存:

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; 
    } 
} 

發送無緩存頭:

$ 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始終是當前的時間。

0

記住設置sendfile off;或緩存頭不起作用。 我用這個剪掉:

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

我對當地的開發工作,關閉所有瀏覽器緩存以下的Nginx虛擬主機(靜態內容):

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; 
     } 
    }