CSS和JS文件没有被更新,据说是因为Nginxcaching

我有我的networking应用程序与AppCache合作,我希望当我修改我的HTML / CSS / JS文件,然后更新我的caching清单,当用户访问我的Web应用程序,他们将有一个更新版本的这些文件。 如果我更改了一个HTML文件,它完美的工作,但是当我更改CSS和JS文件时,旧版本仍在使用中。

我一直在检查一切,我认为这与我的nginxconfiguration有关。 我有一个cache.conf文件,其中包含以下内容:

gzip on; gzip_types text/css application/x-javascript text/x-component text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location ~ \.(css|js|htc)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; } location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ { expires 3600s; add_header Pragma "public"; add_header Cache-Control "max-age=3600, public, must-revalidate, proxy-revalidate"; } 

而在default.conf中我有我的位置。 我想这个caching在除了一个地方的所有位置上工作,我怎么能configuration这个? 我试过以下,它不工作:

 location /dir1/dir2/ { root /var/www/dir1; add_header Pragma "no-cache"; add_header Cache-Control "private"; expires off; } 

谢谢

更新:

我的cache.conf现在看起来像这样:

 location ^~ /desk/ { add_header Pragma "no-cache"; add_header Cache-Control "private"; expires off; } location ^~ /dev/desk/ { add_header Pragma "no-cache"; add_header Cache-Control "private"; expires off; } gzip on; gzip_types text/css application/x-javascript text/x-component text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location ~ \.(css|js|htc)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; } 

nginx中的位置定义了独占configuration,也就是具有两个位置

 location /dir1/dir2/ { # configuration A } location ~ \.(css|js|htc)$ { # configuration B } 

只有一个configuration将用于请求。 有了上述位置(如你的configuration),对/dir1/dir2/file.css的请求将被selectlocation ~ \.(css|js|htc)$作为每个位置select规则 。

如果要求nginx不要search由正则expression式给出的位置(如果请求以/dir1/dir2/开头),则使用^~修饰符:

 location ^~ /dir1/dir2/ { # configuration A } location ~ \.(css|js|htc)$ { # configuration B } 

这样, location ^~ /dir1/dir2/ location将被用于/dir1/dir2/file.css请求。

另外,你可以隔离你的正则expression式位置在另一个前缀位置,例如location / ,像这样:

 location / { # configuration for normal files under / location ~ \.(css|js|htc)$ { # configuration for css/js/htc files under / } } location /dir1/dir2/ { # configuration for all files under /dir1/dir2/ } 

有关更多详细信息,请参阅http://nginx.org/r/location 。 nginx如何处理请求文章也可能有帮助。