使用 node.js 提供静态 gzip 文件 [英] Serve static gzip files using node.js

查看:28
本文介绍了使用 node.js 提供静态 gzip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于开销,我不想使用动态 gzip 的库.

I don't want to use a library which gzips on the fly, because of the overhead.

该网站有一些在 node.js 中实现的动态组件.

The website has some dynamic components which is implemented in node.js.

我有一些静态 js 和 css 文件以及它们的 gzip 对应文件.我只想将 gzip 压缩版本提供给支持它的浏览器.

I have some static js and css files as well as their gzipped counterparts. I want to serve the gzipped version only to browsers which support it.

我考虑使用 express 中的静态中间件来提供静态文件,以及一些 URL 重写中间件来有条件地提供 gzipped 文件.但是,我找不到任何条件重写模块.

I considered using the static middleware in express to serve the static files, along with some URL rewriting middleware to conditionally serve the gzipped files. However, I cannot find any conditional rewrite module.

我无法相信没有人这样做过,或者它需要这么多变通办法.我错过了什么?

I cannot believe that no one has done this, or that it needs so many work arounds. What am I missing?

另一方面,通过 node.js 提供静态文件是否太昂贵?另一方面,将 Apache 用于静态文件并在后面运行 node.js 似乎也很糟糕.AWS EC2 托管最不愚蠢的配置是什么?

On a different note, is serving up static files via node.js too expensive? On the other hand, using Apache for static files and running node.js behind seems bad as well. What is the least stupid configuration for AWS EC2 hosting?

推荐答案

如果您想为客户提供最佳性能,只需使用 CDN.它将为您和许多其他东西处理 gzipping.如果您需要帮助,可以使用 express-cdn 模块.

If you want to have best performance for your clients, just use a CDN. It will take care of gzipping for you and a lot of other stuff. If you need help you can use express-cdn module.

如果您出于某些原因不喜欢 CDN,最好的办法是使用 nginx.我看到它在你的问题中被标记了,但你没有提到任何关于它的内容.nginx 比 node.js 快得多.对于 nginx,您可以检查它是 gzip-static module.

If you don't like CDNs for some reasons, your best bet is using nginx. I see it tagged in your question, but you didn't mention anything about it. nginx is way faster than node.js. For nginx you can check it's gzip-static module.

如果你仍然想使用 node.js,那么 connect-gzip-static是你最好的选择.它的工作原理与 nginx-gzip-static 模块几乎相同.如果请求的文件有 .gz 文件副本,那么它将使用它,否则它将回退到普通的静态连接模块.

If you still want to use node.js, then connect-gzip-static is your best bet. It works almost same as nginx-gzip-static module. If there's a .gz file counterpart of the requested file, then it will use that, else it will fallback to normal connect-static module.

不要忘记事先编译文件,如果你使用gulp,那么你也可以使用gulp-压缩包.如果不只是使用 gzip 命令.

Don't forget to compile files beforehand, if you are using gulp, then you might also use gulp-gzip. If not just use gzip command.

还有 gzippo,它可以即时 gzip,但也会将结果缓存在内存中.所以它只会被 gzip 一次.

There's also gzippo, it gzips on the fly, but it also caches the result in the memory. So it will be only gzipped once.

这篇关于使用 node.js 提供静态 gzip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆