简体   繁体   English

使用Nginx / Node版本控制服务静态文件的最佳方法

[英]Best way to serve static files using versioning with nginx / node

I actually have: 我实际上有:

  1. Nginx running to serve static files (css, js, etc.) Nginx正在运行以提供静态文件(css,js等)
  2. Node with express.js, template engine: ECT (I may change for Swig) 带有express.js的节点,模板引擎:ECT(我可能会更改为Swig)

I am currently trying to find the best way to distribute static files with a custom prefix with versioning: 我目前正在尝试寻找最佳方式来分发带有自定义前缀和版本控制的静态文件:

So to do that, I only set a variable containing the prefix (which depends on the environment). 因此,我只设置了一个包含前缀的变量(取决于环境)。 Then for each request, I set add the prefix to locals in an express middleware in order to access this variable in any html template: 然后,对于每个请求,我设置将前缀添加到快速中间件中的本地变量,以便在任何html模板中访问此变量:

this.use(function(req, res, next) {
    res.locals.staticPrefix = staticPrefix;
    next();
});

But since I also want these static files to be also cached by the client's browser, Nginx serves these files with expire = 30d. 但是由于我也希望这些静态文件也可以由客户端的浏览器缓存,因此Nginx为这些文件提供expire = 30d。

Now to force a client to retrieve a static file (if it has changed for example), I need to provide static urls with a dynamic url parameter. 现在要强制客户端检索静态文件(例如,如果它已更改),我需要为静态URL提供一个动态url参数。

My first idea would be to set a version variable when I start the nodejs app to append it to the final url: 我的第一个想法是在启动nodejs应用程序时设置一个版本变量,以将其附加到最终URL:

var staticVersion = new Date().getTime();

So in the html template, the final url for a 'myFile.css' would like this: staticPrefix + 'myFile.css?' + staticVersion 因此,在html模板中,“ myFile.css”的最终网址应为: staticPrefix + 'myFile.css?' + staticVersion staticPrefix + 'myFile.css?' + staticVersion

In this case, I only need to restart the nodejs application when one of the static files has been updated. 在这种情况下,仅当其中一个静态文件已更新时,才需要重新启动nodejs应用程序。 It will make the url to change (according to the new date) and the client to do a new request of the file. 它将使url更改(根据新日期),并使客户端对文件进行新请求。

Is there a better way to handle this situation with node? 有没有更好的方法来处理这种情况?

Best way to handle static assets like css/js files is to minify them in production. 处理诸如CSS / JS文件之类的静态资产的最佳方法是在生产中最小化它们。 Use file name based on file contents. 根据文件内容使用文件名。 This way every time you change anything in js/css files, the minification code will take care of generating new file if needed. 这样,每次您更改js / css文件中的任何内容时,如果需要,缩小代码将负责生成新文件。 You can hook minification script to run post deployment. 您可以挂钩缩小脚本以运行后期部署。

I have written a package smush to help with minification tasks. 我已经写了一包斯马什以帮助缩小任务。 Head onto its github page for example usage and sample codes. 转至github页面以获取示例用法和示例代码。

You could use other tools/package for minification if it suits better to your use case. 如果更适合您的用例,则可以使用其他工具/软件包进行缩小。

Coming back to your question, you can set the root dir for nginx to static dir of your node server( /path/to/node/server/public ?). 回到您的问题,您可以将nginx的根目录设置为节点服务器的静态目录( /path/to/node/server/public ?)。 This way nginx will cache and serve your static files. 这样,nginx将缓存并提供您的静态文件。 The node server will not be bothered to serve the static assets afterwards. 之后,节点服务器将不再为静态资产服务。

Let me know if this makes sense or if you need any further clarification. 让我知道这是否有意义,或者您是否需要进一步说明。

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

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