我在 nginx 1.2.8 上启用了 gzip,但出于某种原因,它没有压缩任何东西。
Nginx 安装信息:
nginx version: nginx/1.2.8
built by gcc 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.2.8 --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-pcre --conf-path=/etc/nginx/nginx.conf --add-module=../headers-more-nginx-module-0.19rc1
配置:
user www-user;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 10M;
sendfile on;
keepalive_timeout 65;
more_clear_headers "Server";
gzip on;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
server {
server_name test.com;
root /www;
index index.php index.html index.htm;
listen 80 default_server;
rewrite ^/(.*)/$ /$1 permanent; #remove trailing slash
#charset koi8-r;
#access_log logs/host.access.log main;
include general/*.conf;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
我正在尝试发送一个简单的静态 css 文件。在我的 nginx 配置中,text/css
包含在gzip_types
.
当我尝试使用 Firefox 获取文件时,fiddler 显示接收到的内容未经过 gzip 压缩:
请求标头:
GET http://test.com/test.css HTTP/1.1
Host: nextdreamtest2.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __utma=237624223.1052931239.1362029000.1365467381.1365469205.16; __utmz=237624223.1362029000.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utmc=237624223; __utmb=237624223
Proxy-Authorization: Basic cHJveHk6RTRRWlNlY0JLU3o0OFh4cWpLNkg=
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
响应标头:
HTTP/1.1 200 OK
Date: Tue, 09 Apr 2013 01:35:07 GMT
Content-Type: text/css
Last-Modified: Thu, 31 May 2012 08:50:00 GMT
Connection: keep-alive
Content-Length: 1688
请注意,即使在服务器上启用了 gzip,也没有Content-Encoding: gzip
标头,并且内容类型是配置为 gzip 的类型。
经过更多的调查,我现在更加困惑了。我在运行 nginx 的机器上尝试了 FireFox 20.0(Ubuntu 12.10),并且文件被正确压缩:
Connection keep-alive
Content-Encoding gzip <------------------------------
Content-Type text/html
Date Tue, 09 Apr 2013 04:23:08 GMT
Set-Cookie 200ceb26807d6bf99fd6f4f0d1ca54d4=7hjir1h44g3dq8hlsihdsrn9v5; path=/; HttpOnly
Transfer-Encoding chunked <---------------------
但是,如果我尝试使用 IE10、FireFox 20 和 Chrome 26 以及 Fiddler 检查我的 Windows 8 计算机上的标头以访问服务器,响应永远不会压缩!
Connection keep-alive
Content-Length 2192 <------------------
Content-Type text/html
Date Tue, 09 Apr 2013 04:27:16 GMT
这非常奇怪,因为我在我的 nginx.conf 中没有看到任何会歧视 Windows 8 机器的东西。
是什么原因造成的?
在看到 Ubuntu 上的 FireFox 20 能够接收 gzip 压缩的内容而我的 Windows 8 机器不能接收后,我开始怀疑了。
它在我的 Windows 8 机器上尝试了以下:IE10、FireFox 20、Chrome 26、fiddler 和 curl。当他们显示他们收到了非 gzip 压缩的内容时,我立即怀疑我的互联网安全套件。
卸载 BitDefender Windows 8 安全软件后(它也没有最佳性能),我现在可以看到我的浏览器和 Windows 8 机器上的客户端接收 gzip 压缩的内容。
BitDefender 可能会解压缩我的数据包并在它攻击任何客户端之前分析它们是否有病毒和网络钓鱼材料。
尽管如此,在为此浪费了将近一天之后,我不会再安装它,而是会考虑另一种产品。
尝试将默认类型更改为
text/html
- 的默认类型application/octet-stream
不可压缩。