如何在Web服务器上设置gzip压缩?

时间:2022-12-25 19:55:26

I have an embedded webserver that has a total of 2 Megs of space on it. Normally you gzip files for the clients benefit, but this would save us space on the server. I read that you can just gzip the js file and save it on the server. I tested that on IIS and I didn't have any luck at all. What exactly do I need to do on every step of the process to make this work?

我有一个嵌入式网络服务器,总共有2兆的空间。通常你为客户端gzip文件带来好处,但这会节省我们在服务器上的空间。我读到你可以只是gzip js文件并将其保存在服务器上。我在IIS上测试过,我根本没有运气。在完成这项工作的每个步骤中,我到底需要做什么?

This is what I imagine it will be like:

这就是我想象的那样:

  1. gzip foo.js
  2. change link in html to point to foo.js.gz instead of just .js
  3. 将html中的链接更改为指向foo.js.gz而不仅仅是.js

  4. Add some kind of header to the response?
  5. 在响应中添加某种标题?

Thanks for any help at all.

谢谢你的帮助。

-fREW

EDIT: My webserver can't do anything on the fly. It's not Apache or IIS; it's a binary on a ZiLog processor. I know that you can compress streams; I just heard that you can also compress the files once and leave them compressed.

编辑:我的网络服务器无法动态执行任何操作。它不是Apache或IIS;它是ZiLog处理器上的二进制文件。我知道你可以压缩流;我刚刚听说你也可以压缩文件一次并让它们压缩。

3 个解决方案

#1


5  

As others have mentioned mod_deflate does that for you, but I guess you need to do it manually since it is an embedded environment.

正如其他人提到的那样,mod_deflate会为你做这件事,但我想你需要手动完成,因为它是一个嵌入式环境。

First of all you should leave the name of the file foo.js after you gzip it.

首先,你应该在gzip之后留下文件名foo.js。

You should not change anything in your html files. Since the file is still foo.js

你不应该改变你的html文件中的任何内容。由于该文件仍然是foo.js

In the response header of (the gzipped) foo.js you send the header

在(gzip)foo.js的响应头中,您发送标头

Content-Encoding: gzip

This should do the trick. The client asks for foo.js and receives Content-Encoding: gzip followed by the gzipped file, which it automatically ungzips before parsing.

这应该可以解决问题。客户端请求foo.js并接收Content-Encoding:gzip后跟gzip文件,它在解析之前会自动解压缩。

Of course this assumes your are sure the client understands gzip encoding, if you are not sure, you should only send gzipped data when the request header contains

当然这假设你确定客户端理解gzip编码,如果你不确定,你应该只在请求头包含时发送gzip数据

Accept-Encoding: gzip

#2


1  

Using gzip compression on a webserver usually means compressing the output from it to conserve your bandwidth - not quite what you have in mind.

在网络服务器上使用gzip压缩通常意味着压缩它的输出以节省带宽 - 不是你想要的。

Look at this description or This example

请看这个描述或这个例子

#3


1  

If you're using Apache, you use mod_deflate, and it compresses on the fly.

如果您正在使用Apache,则使用mod_deflate,它会动态压缩。

I think you're getting confused by thinking that if you gzip something it has to be a file. Instead, think about how a file is just a stream of data, and that stream of data can get compressed here, transmitted, and uncompressed there without the client having to even think about it.

我认为如果你gzip它必须是一个文件,你会感到困惑。相反,考虑一个文件如何只是一个数据流,并且该数据流可以在这里被压缩,传输和解压缩,而客户端甚至不必考虑它。

#1


5  

As others have mentioned mod_deflate does that for you, but I guess you need to do it manually since it is an embedded environment.

正如其他人提到的那样,mod_deflate会为你做这件事,但我想你需要手动完成,因为它是一个嵌入式环境。

First of all you should leave the name of the file foo.js after you gzip it.

首先,你应该在gzip之后留下文件名foo.js。

You should not change anything in your html files. Since the file is still foo.js

你不应该改变你的html文件中的任何内容。由于该文件仍然是foo.js

In the response header of (the gzipped) foo.js you send the header

在(gzip)foo.js的响应头中,您发送标头

Content-Encoding: gzip

This should do the trick. The client asks for foo.js and receives Content-Encoding: gzip followed by the gzipped file, which it automatically ungzips before parsing.

这应该可以解决问题。客户端请求foo.js并接收Content-Encoding:gzip后跟gzip文件,它在解析之前会自动解压缩。

Of course this assumes your are sure the client understands gzip encoding, if you are not sure, you should only send gzipped data when the request header contains

当然这假设你确定客户端理解gzip编码,如果你不确定,你应该只在请求头包含时发送gzip数据

Accept-Encoding: gzip

#2


1  

Using gzip compression on a webserver usually means compressing the output from it to conserve your bandwidth - not quite what you have in mind.

在网络服务器上使用gzip压缩通常意味着压缩它的输出以节省带宽 - 不是你想要的。

Look at this description or This example

请看这个描述或这个例子

#3


1  

If you're using Apache, you use mod_deflate, and it compresses on the fly.

如果您正在使用Apache,则使用mod_deflate,它会动态压缩。

I think you're getting confused by thinking that if you gzip something it has to be a file. Instead, think about how a file is just a stream of data, and that stream of data can get compressed here, transmitted, and uncompressed there without the client having to even think about it.

我认为如果你gzip它必须是一个文件,你会感到困惑。相反,考虑一个文件如何只是一个数据流,并且该数据流可以在这里被压缩,传输和解压缩,而客户端甚至不必考虑它。