2016-10-25 15 views
6

Ok, tôi gần như đã từ bỏ việc này, nhưng làm cách nào để vô hiệu bộ nhớ đệm từ Nginx cho các tệp JavaScript? Tôi đang sử dụng một container docker với Nginx. Khi tôi bây giờ thay đổi một cái gì đó trong tập tin JavaScript, tôi cần nhiều tải lại cho đến khi tập tin mới có.Tắt bộ nhớ cache nginx cho các tệp JavaScript

Làm cách nào để biết đó là Nginx chứ không phải trình duyệt/đế cắm?

Trình duyệt: Tôi đã sử dụng curl trên dòng lệnh để mô phỏng yêu cầu và có cùng vấn đề. Ngoài ra, tôi đang sử dụng plugin CacheKiller và đã tắt bộ nhớ cache trong Công cụ dành cho Chrome Dev.

Đế cắm: Khi tôi kết nối với hộp đựng của thùng chứa và sử dụng cat sau khi thay đổi tệp, tôi nhận được kết quả chính xác ngay lập tức.

tôi đã thay đổi tôi nginx.conf cho sites-enabled này (mà tôi tìm thấy trong một thread 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; 
} 

Tuy nhiên, sau khi xây dựng lại các container (và đảm bảo nó trong container với cat), nó vẫn didn 't làm việc. Ở đây là hoàn tất các .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; 
} 

Trả lời

3

Các expiresadd_header chỉ thị không có ảnh hưởng trên Nginx bộ nhớ đệm các tập tin, đó là hoàn toàn về những gì các trình duyệt thấy.

gì bạn có thể muốn thay thế là:

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

Mặc dù thường .js vv là điều bạn sẽ bộ nhớ cache, vì vậy có lẽ bạn chỉ nên vô hiệu hóa bộ nhớ đệm hoàn toàn?

+0

Đó là okay cho tôi để cache nó trong sản xuất, không phát triển. Tôi sẽ thử đề xuất của bạn – Musterknabe

+0

Gotcha, có ý nghĩa sau đó. –

+0

Tôi vừa thêm mã của bạn vào 'app.conf', tuy nhiên bây giờ nginx không còn bắt đầu nữa.Khi tôi sử dụng 'nginx -t' tôi nhận được lỗi 'chỉ thị không xác định' proxy_no_store" ' – Musterknabe

2

gì bạn đang tìm kiếm là một chỉ thị đơn giản như:

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

trên sẽ không cache các phần mở rộng trong phạm vi(). Bạn có thể cấu hình các chỉ thị khác nhau cho các loại tệp khác nhau.

12

tôi có các máy chủ sau nginx ảo (nội dung tĩnh) cho công việc phát triển địa phương để vô hiệu hóa tất cả các bộ nhớ đệm trình duyệt:

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

Không tiêu đề bộ nhớ cache gửi:

$ 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 luôn là thời điểm hiện tại.

0

Hãy nhớ đặt sendfile off; hoặc tiêu đề bộ nhớ cache không hoạt động. tôi sử dụng này snipped:

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

Tôi có Nginx máy chủ ảo sau (nội dung tĩnh) cho công việc phát triển địa phương để vô hiệu hóa tất cả các bộ nhớ đệm trình duyệt:

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; 
     } 
    } 
Các vấn đề liên quan