服务器端自动缩小?

发布于 2024-09-03 03:35:23 字数 163 浏览 4 评论 0原文

有没有办法自动缩小静态内容,然后自动从缓存中提供它?类似于 mod_compress/mod_deflate 工作吗?最好是我可以与压缩结合使用的东西(因为压缩有更明显的好处)。

我更喜欢与 lighttpd 一起使用的东西,但我还没有找到任何东西,所以任何可以做到这一点的网络服务器都会很有趣。

Is there any way to automatically minify static content and then serve it from a cache automatically? Similar to have mod_compress/mod_deflate work? Preferably something I could use in combination with compression (since compression has a more noticeable benefit).

My preference is something that works with lighttpd but I haven't been able to find anything, so any web server that can do it would be interesting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷血 2024-09-10 03:35:23

您可以尝试 nginx 的第三方 Strip 模块:

http://wiki.nginx.org/NginxHttpStripModule

您的任何模块use 只是删除空格。通过使用能够理解您正在缩小的内容的缩小器,您将获得更好的结果。例如 Google 的 Closure javascript 编译器。

它足够聪明,可以知道变量是什么并使其名称更短。空白删除器无法做到这一点。

我建议缩小离线范围,除非您的网站流量非常低。但如果你想在你的实际环境中缩小,我建议使用 nginx 的代理缓存。 (抱歉,但我没有足够的声誉来发布多个链接)

或者您可以查看 memcached 来获取内存缓存,或者使用 Redis 来获取相同的功能,但需要磁盘备份。

You can try nginx's third party Strip module:

http://wiki.nginx.org/NginxHttpStripModule

Any module you use is just going to remove whitespace. You'll get a better result by using a minifier that understands whatever you're minifying. e.g. Google's Closure javascript compiler.

It's smart enough to know what a variable is and make it's name shorter. A whitespace remover can't do that.

I'd recommend minifying offline unless your site is very low traffic. But if you want to minify in your live environment I recommend using nginx's proxy cache. (Sorry but I don't have enough reputation to post more than one link)

Or you can look into memcached for an in-memory cache or Redis for the same thing but with disk backup.

老娘不死你永远是小三 2024-09-10 03:35:23

我决定通过 PHP 来完成此操作(主要是因为我不想编写 lighttpd 模块)。

我的脚本接受一个查询字符串,指定所请求的文件的类型(js 或 css),然后是这些文件的名称。例如,在我的网站上,CSS 添加如下:

这缩小并连接 style.css 和 blue.css

它使用 JSMin-PHPcssmin

如果可用的话,它还会使用 XCache 缓存文件(因为缩小成本很高)。我实际上计划更改脚本,以便在 Xcache 不可用时它不会缩小,但我有 Xcache 并且我感到无聊。

无论如何,如果其他人想要它,就在这里。如果您使用我的,则需要更改 isAllowed() 函数来列出您的文件(使其仅返回 true 可能是安全的,但仅列出我想要的文件很容易允许)。

I decided to do this through PHP (mostly because I didn't feel like writing a lighttpd module).

My script takes in a query string specifying the type of the files requested (js or css), and then the names of those files. For example, on my site the CSS is added like this:

<link rel="stylesheet" href="concat.php?type=css&style&blue" ... />

This minifies and concatenates style.css and blue.css

It uses JSMin-PHP and cssmin.

It also caches the files using XCache if it's available (since minifying is expensive). I actually plan to change the script so it doesn't minify if Xcache isn't available, but I have Xcache and I got bored.

Anyway, if anyone else wants it, it's here. If you use mine you'll need to change the isAllowed() function to list your files (it may be safe to make it just return true, but it was easy to just list the ones I want to allow).

并安 2024-09-10 03:35:23

我使用 Microsoft Ajax Minifier ,它附带一个 C# 库来缩小 js 文件。我在服务器上使用它,并在每页上最多提供两个缩小的 .js 文件(一个“静态”文件在整个站点上相同,一个“动态”文件特定于该页面)。

Yahoo 的 YUI 压缩器也是一个简单的 Java .jar 文件,您也可以使用它。

我认为重要的是不要逐个文件地进行操作。您确实需要组合 .js 文件才能获得最大收益。因此,“自动”解决方案实际上不会起作用 - 因为它必然只能在逐个文件的基础上起作用。

I use Microsoft Ajax Minifier which comes with a C# library to minify js files. I use that on the server and serve up a maximum of two minified .js files per page (one "static" one that is the same across the whole site, and one "dynamic" one that is specific to just that page).

Yahoo's YUI compressor is also a simple Java .jar file that you could use as well.

The important thing, I think, is not to do it on a file-by-file basis. You really do need to combine the .js files to get the most benefit. For that reason, an "automatic" solution is not really going to work - because it will necessarily only work on a file-by-file basis.

自此以后,行同陌路 2024-09-10 03:35:23

如果您使用 Nginx 而不是 lighttpd,那么您可以利用 Nginx 的 嵌入式 Perl 支持 以利用 Perl 模块 JavaScript-Minifier 用于缩小和缓存 JS 服务器端。

以下是如何实现此目的的详细信息:wiki.nginx.org/NginxEmbeddedPerlMinifyJS

If you use Nginx instead of lighttpd then you can take advantage of Nginx's embedded Perl support to leverage the Perl module JavaScript-Minifier to minify and cache JS server-side.

Here are the details on how to achieve this: wiki.nginx.org/NginxEmbeddedPerlMinifyJS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文