我怎样才能让Nginx告诉Firefoxcaching我的内容?

以下标题(来自静态媒体响应)不会导致在Firefox中进行caching。 在Chrome中他们这样做。

HTTP/1.1 200 OK Server: nginx Date: Sat, 22 Dec 2012 21:20:39 GMT Content-Type: application/x-javascript; charset=utf-8 Last-Modified: Fri, 21 Dec 2012 19:28:54 GMT Transfer-Encoding: chunked Connection: keep-alive Cache-Control: public, max-age=86400 Content-Encoding: gzip 

我的Nginx服务器的静态内容如下所示:

 server { listen 80; server_name static.example.com; # Logs access_log /var/log/nginx/static.example.com.access.log; error_log /var/log/nginx/static.example.com.error.log; # Root location / { alias /var/www/app/deps/current/repo/src/example/static/; add_header Cache-Control "public, max-age=86400"; } } 

我也试过使用expires 24h; ,代替add_header ... ,没有运气。

我在互联网上发现了许多类似的投诉,但没有解决scheme,也没有任何关于为什么这么做的想法,除了一个人提到Firefox故意偏离处理HTTP 1.1 Cache-Control头的规范。

有什么办法让火狐caching我的静态媒体,通过一个头或多个头? 我不希望我的服务器的静态媒体请求的75%来自我的用户的20%,只是因为Firefox被捣毁。

请注意,我使用默认设置的Firefox 17.0.1,但安装了Firebug并打开Net标签页。

更新:

使用,而不是:

 expires 1d; add_header Cache-Control public; 

使标题包含:

 Expires: Wed, 26 Dec 2012 19:54:20 GMT Cache-Control: max-age=604800, public 

这也不会导致Firefoxcaching内容。

你如何确定Firefox没有caching你的文件?

当回复中存在Last-Modified头部时,浏览器应该使用If-Modified-Since头部对相同的URL进行后续请求,然后来自服务器的响应可能只包含头部(没有正文)如果该文件还没有被修改。

IIRC,Gecko还会查看Last-Modifieddate的大小。 如果是相对较新的,那么内容总是变化,并且必须经常重新请求(带有If-Modified-Since等)。 另一方面,如果date是几个星期,几个月或者几年,那么内容很可能会被caching很长一段时间。

还有一件事要看:你的火狐浏览器上是否有正确的时区和时间?

 server { expires 1d; add_header Cache-Control public; listen 80; .... } 

请尝试这种方式请像nginx文档中所述


所以我很高兴,并试图caching自己与下面的configuration。 我已经改变了,我开始在本地主机上的应用程序:4567。 而在外部网站configuration代理通行证。 或多或less像我能find的大多数caching实例。

 user nginx; worker_processes 2; error_log /var/log/nginxtesterror.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; proxy_cache_path /var/www/app/deps/current/repo/src/example/static/ levels=1:1:2 keys_zone=one:200m max_size=1000m loader_files=2000 inactive=60m; proxy_temp_path /var/www/app/deps/current/repo/src/example/tmp; proxy_cache_valid 1d; gzip on; gzip_disable msie6; gzip_static on; gzip_comp_level 4; gzip_proxied any; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; server { listen 80; server_name static.example.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass_header Set-Cookie ; location ~* .(?:ico|css|js|gif|jpe?g|png|pdf|zip|tar|t?gz|mp3|wav|swf)$ { expires max; add_header Pragma public; add_header Cache-Control public; } location / { proxy_pass http://localhost:4567; proxy_cache_key $cache_key; proxy_cache one; add_header Cache-Control public; proxy_cache_valid 1d; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_504 http_404; } } server { root /var/www/app/deps/current/repo/src/example/static/; listen 127.0.0.1:4567; set $cache_key $scheme$host$uri$is_args$args; # Logs access_log /var/log/nginx/static.example.com.access.log; error_log /var/log/nginx/static.example.com.error.log; # Root location / { alias /var/www/app/deps/current/repo/src/example/static/; } } }