简体   繁体   English

在nginx中自动版本化CSS / JS

[英]Auto-versioning CSS/JS in nginx

I have a setup where nginx is serving all static content (CSS/JS). 我有一个设置,其中nginx提供所有静态内容(CSS / JS)。 Our problem is that when we update the static content the browser doesn't necessarily update them immediately, causing problems when we're pushing new versions. 我们的问题是,当我们更新静态内容时,浏览器不一定立即更新它们,导致我们推送新版本时出现问题。

I would like to have a nginx plugin that basically replaces all calls to CSS/JS and ads a versioning number, like this: 我想有一个nginx插件基本上取代所有对CSS / JS的调用和广告版本号,如下所示:

Before: 之前:

<link rel="stylesheet" type="text/css" href="/media/css/style.css" />

After: 后:

<link rel="stylesheet" type="text/css" href="/media/css/style.css?3428432" />

And does this automatically based on the latest changed date on the style.css file itself. 并且这是根据style.css文件本身的最新更改日期自动完成的。 So I don't have to update the HTML. 所以我不必更新HTML。 Is there a plugin for this? 有插件吗? I know Googles mod_pagespeed does simliar things in their apache2 module.. but I can't find anyone for nginx. 我知道谷歌mod_pagespeed在他们的apache2模块中做了类似的事情..但我找不到任何人的nginx。

Generally, this is done in the application itself, not at the webserver level. 通常,这是在应用程序本身完成的,而不是在Web服务器级别完成的。 The webserver generally only knows what to serve, and from where. 网络服务器通常只知道要提供什么,从何处。 Both PHP and Rails have the ability to do what you're describing above, but again, that's within the application itself. PHP和Rails都能够执行您在上面描述的内容,但同样,这也是应用程序本身的内容。

From what I can tell, this article is a good step-by-step walk through which is very similar to what you're asking for, but again, this involves application changes. 从我所知道的, 这篇文章是一个很好的循序渐进,与你所要求的非常相似,但同样,这涉及应用程序的变化。 If you search Google for "nginx css versioning" you'll find other articles which discuss the nginx config, but all that I looked at involved application changes as well. 如果您在Google上搜索“nginx css版本控制”,您会找到其他讨论nginx配置的文章,但我所看到的所有文章都涉及应用程序更改。

Adding the below to the nginx should serve the file by taking just the filename and ignoring the version info. 将以下内容添加到nginx应该只通过文件名并忽略版本信息来提供文件。

location ~ ^/(assets/js|assets/css) {
 root path/to/the/static/files;
 access_log off;
 expires max;
 try_files $uri $1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM