2016-10-25 10 views
6

これはほとんど気にしないですが、JavaScript用のNginxファイルからのキャッシュを無効にするにはどうしたらいいですか?私はNginxでドッカーコンテナを使用しています。 JavaScriptファイルで何かを変更すると、新しいファイルがそこに来るまで、複数のリロードが必要になります。JavaScriptファイル用のnginxキャッシュを無効にする

Nginxでブラウザ/ドッカーではないことをどのように知っていますか?

ブラウザ:コマンドラインでcurlを使用してリクエストをシミュレートし、同じ問題が発生しました。また、CacheKillerプラグインを使用しており、Chrome Dev Toolsでキャッシュを無効にしています。

ドッカー:ファイルを変更した後、コンテナのbashに接続してcatを使用すると、すぐに正しい結果が得られます。

私はしかし、コンテナを再構築(と、それはcatと容器の中にあります確認して)した後、それはまだdidnの(私は別のstackoverflowのスレッドで見つけた)この

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

からsites-enabledのための私のnginx.confを変更仕事はありません。これは、ここにexpiresadd_headerディレクティブは、ファイルをキャッシュnginxのに影響を与えない完全な.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

あり、それらは純粋にブラウザが見ているものとしています。

あなたはおそらく代わりにしたいことは次のとおりです。

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

Gotcha、その後意味があります。 –

+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; 
     } 
    } 
関連する問題