简体   繁体   English

使用 nginx 启用 gzip 压缩

[英]enable gzip compression with nginx

I am trying to enable gzip compression for components of my website.我正在尝试为我的网站组件启用 gzip 压缩。 I have ubuntu 11.04 server and nginx 1.2.我有 ubuntu 11.04 服务器和 nginx 1.2。
in my nginx configuration of the website, i have this在网站的 nginx 配置中,我有这个

gzip             on;
#gzip_min_length  1000;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
#it was gzip_buffers 16 8k; 
gzip_buffers 128 4k; #my pagesize is 4
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

and Yslow and google speed measures are advising me to use gzip to reduce transmission over network.和 Yslow 和谷歌速度措施建议我使用 gzip 来减少网络传输。 now when i try to curl -I my_js_file i got现在当我尝试curl -I my_js_file我得到了

curl -I http://www.albawaba.com/sites/default/files/js/js_367664096ca6baf65052749f685cac7b.js
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 14 Apr 2013 13:15:43 GMT
Content-Type: application/x-javascript
Content-Length: 208463
Connection: keep-alive
Last-Modified: Sun, 14 Apr 2013 10:58:06 GMT
Vary: Accept-Encoding
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Pragma: public
Cache-Control: public
Accept-Ranges: bytes

any idea of what i have done wrong or what shall i do to get compressed content?知道我做错了什么,或者我应该怎么做才能获得压缩内容?

As others have written, it's not enough to enable gzip compression in your server -- the client also needs to ask for it in its requests via the Accept-Encoding: gzip header (or a superset thereof).正如其他人所写,在您的服务器中启用 gzip 压缩是不够的——客户端还需要通过Accept-Encoding: gzip标头(或其超集)在其请求中要求它。 Modern browsers include this header automatically, but for curl you'll need to include one of the following in your command:现代浏览器会自动包含此标头,但对于 curl,您需要在命令中包含以下内容之一:

  • -H "Accept-Encoding: gzip" : You should see the Content-Encoding: gzip header in the response (might need to output headers with curl's -v flag), as well as some seemingly garbled output for the content, the actual gzip stream. -H "Accept-Encoding: gzip" :您应该在响应中看到Content-Encoding: gzip标头(可能需要使用 curl 的-v标志输出标头),以及一些看似乱码的内容输出,实际的 gzip溪流。
  • --compressed : You should still see Content-Encoding: gzip in the response headers, but curl knows to decompress the content before outputting it. --compressed :您仍然应该在响应标头中看到Content-Encoding: gzip ,但 curl 知道在输出内容之前对其进行解压缩。

I can't find anything obviously wrong with your config, usually gzip on & gzip_types application/x-javascript would be enough to get you going.我找不到您的配置有任何明显错误,通常 gzip on & gzip_types application/x-javascript 足以让您继续前进。 If everything is working right you'll get a "Content-Encoding:gzip" returned back to you.如果一切正常,您将收到一个“Content-Encoding:gzip”返回给您。

PLEASE KEEP IN MIND: I have much more consistency with GOOGLE DEVELOPER TOOLS (curl just doesn't behave the way a browser would).请记住:我与 GOOGLE DEVELOPER TOOLS 的一致性更高(curl 的行为与浏览器不同)。

In Chrome, right click and go to "inspect element" then go to "network" (then reload the page if you have to), then click on a resource and check the header tab, the output should look like this (notice the content-encoding is gzip, yay):在 Chrome 中,右键单击并转到“检查元素”,然后转到“网络”(如果需要,然后重新加载页面),然后单击资源并检查标题选项卡,输出应如下所示(注意内容- 编码是 gzip,是的):

Request URL:https://ssl.gstatic.com/gb/js/sem_a3becc1f55aef317b63a03a400446790.js
Request Method:GET
Status Code:200 OK (from cache)
Response Headersview source
age:199067
cache-control:public, max-age=691200
content-encoding:gzip
content-length:19132
content-type:text/javascript
date:Fri, 12 Apr 2013 06:32:58 GMT
expires:Sat, 20 Apr 2013 06:32:58 GMT
last-modified:Sat, 23 Mar 2013 01:48:21 GMT
server:sffe
status:200 OK
vary:Accept-Encoding
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block

Anyway if you are SURE your content is not getting gzipped, I normally get up and running pretty fast with the following:无论如何,如果您确定您的内容没有被压缩,我通常会通过以下方式快速启动和运行:

## Compression
gzip              on;
gzip_buffers      16 8k;
gzip_comp_level   4;
gzip_http_version 1.0;
gzip_min_length   1280;
gzip_types        text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp;
gzip_vary         on;

You could try this in replacement for your code, and/or tweak your values one at a time to help you localize your issue.您可以尝试用它来代替您的代码,和/或一次调整一个值以帮助您定位问题。

Remember to restart or reload nginx after changing the config.更改配置后记得重启或重新加载nginx。

It may also be useful to check your logs and see if there's anything interesting there should you still be stuck.如果您仍然被卡住,检查您的日志并查看是否有任何有趣的事情也可能很有用。

I just changed gzip_http_version 1.1;我刚刚更改了gzip_http_version 1.1; to be gzip_http_version 1.0;gzip_http_version 1.0; and then it worked然后它起作用了

I had to enable gzip in my /etc/nginx/nginx.conf configuration:我必须在我的/etc/nginx/nginx.conf配置中启用 gzip:

gzip on;
gzip_disable "msie6";

gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Please note that I had to add application/javascript to the standard gzip_types configuration.请注意,我必须将application/javascript添加到标准gzip_types配置中。

You need to run:你需要运行:

curl -I --compressed my_js_file

to make curl send an Accept-Encoding header for gzip - the server will only compress content if the client sends a header saying it will accept it.使 curl 为 gzip 发送 Accept-Encoding 标头 - 如果客户端发送标头表示将接受它,服务器只会压缩内容。

NB you can write:注意你可以写:

gzip_disable "msi6"

rather than using a regex to disable in IE 5.5 and 6, and you needn't specify text/html as a type because it is always compressed as long as gzip is activated.而不是在 IE 5.5 和 6 中使用正则表达式禁用,并且您不需要将 text/html 指定为类型,因为只要 gzip 被激活它总是被压缩。

I am just taking a guess here, but I think you may have to increase your gzip buffer size.我只是在这里猜测,但我认为您可能需要增加 gzip 缓冲区大小。

Here are the files that the browser pulls down from the domain.以下是浏览器从域中提取的文件。 The number on the right is the file download size.右边的数字是文件下载大小。

从域提供的文件

You may not be able to tell from the screen shot, but all of the text content files ARE gzipped, except for the js file you mention in your question.您可能无法从屏幕截图中看出,但除了您在问题中提到的 js 文件之外,所有文本内容文件都是 gzipped。 In the screenshot the js file is the file in green, with a size of about 200K.截图中js文件为绿色文件,大小约200K。 This file size is greater than what you have specified for your gzip buffers (128K).此文件大小大于您为 gzip 缓冲区指定的大小 (128K)。

The Gzip module docs do not really give a good indication as to what the gzip buffers are used for (whether the buffers are used for uncompressed or compressed data). Gzip 模块文档并没有很好地说明 gzip 缓冲区的用途(缓冲区是用于未压缩还是压缩数据)。 However, the following post seems to indicate that the buffer size should be greater than the uncompressed file size: Large files with NGINX, GZip, and SSL但是,以下帖子似乎表明缓冲区大小应大于未压缩文件的大小: 使用 NGINX、GZip 和 SSL 的大文件

Here is my nginx configuration and it works.这是我的 nginx 配置,它可以工作。

gzip                on;
gzip_min_length     1000;
gzip_buffers        4 8k;
gzip_http_version   1.0;
gzip_disable        "msie6";
gzip_types          text/plain text/css application/json application/javascript application/x-javasc    ript text/xml application/xml application/xml+rss text/javascript;
gzip_vary           on;

I think the keypoints are gzip , gzip_disable and gzip_types .我认为关键点是gzipgzip_disablegzip_types

Just like Alaa I had to add gzip_http_version 1.0;就像 Alaa 我不得不添加gzip_http_version 1.0; (no version was previously specified) for it to work (I tried on Firefox 27.0.0). (之前没有指定版本)让它工作(我在 Firefox 27.0.0 上试过)。

I've experienced the same problem as Alaa, and the problem is caused by Antivirus software, that is currently installed on my computer.我遇到了与 Alaa 相同的问题,该问题是由当前安装在我计算机上的防病毒软件引起的。

Proxy servers and anti-virus software can disable compression when files are downloaded to a client machine.将文件下载到客户端计算机时,代理服务器和防病毒软件可以禁用压缩。 So if you are running web site in a browser on a client machine that is using such anti-virus software, or that sits behind an intermediate proxy server (many proxies are transparent, and you may not even be aware of a proxy intervening between your client and web server), they may be the cause of this issue.因此,如果您在使用此类防病毒软件的客户端计算机上的浏览器中运行网站,或者位于中间代理服务器后面(许多代理是透明的,您甚至可能不知道代理之间的干预)客户端和 Web 服务器),它们可能是导致此问题的原因。

Disabling antivirus solved my problem with browsers and you don't even need to set gzip_http_version to 1.0.禁用防病毒软件解决了我的浏览器问题,您甚至不需要将 gzip_http_version 设置为 1.0。

Hope that will help you.希望这会帮助你。

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

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