为什么Nginx的Gzip预压缩模块不工作?

我目前正试图设置Nginx来服务我所有的静态文件。 由于他们不会经常更改我想我们gzip_static模块允许我预先gzip我的文件的副本,以节省一些CPU时间,并允许更好的压缩。

我用--with-http_gzip_static_module编译Nginx并设置它,以便它为我的静态文件提供服务,到目前为止没有问题。 我想testing并确保静态gzip实际上工作,所以我做了两个文件, test.txttest.txt.gz 每个文件的第一行表示是否被压缩,然后是一个换行符和256个随机字符(两个文件不同)。

我读过文件的修改时间和它的gzip对应应该是一样的,我已经尝试了以下两种:

 touch test.* touch -r test.txt test.txt.gx 

在我的本地机器上,我用curltesting:

 curl $URL/test.txt 

这工作正常,我回来的版本,我没有预先压缩,但是当我这样做:

 curl -H "Accept-Encoding: gzip" $URL/test.txt | gunzip 

回来了我没有预压缩的版本。 我尝试在我的nginx.conf设置gzip off ,但是没有什么区别。 我也用--without-http_gzip_module重新编译了Nginx,而且似乎也没有什么区别,Nginx 仍然在忙于自己的工作。

我对Nginx来说相当陌生,但是我真的很茫然。

这是./nginx -V的输出

 built by gcc 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) configure arguments: --sbin-path=$SOMEPATH/nginx --prefix=$SOMEPATH --user=$ME --group=$MYGROUP --with-http_gzip_static_module --without-http_gzip_module 

这是我的nginx.conf

 worker_processes 1; events { worker_connections 1024; } error_log logs/error.log; pid logs/nginx.pid; http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; gzip_static on; sendfile on; keepalive_timeout 65; access_log logs/access.log; server { listen XXXX; server_name foo.bar.com; location / { root html; } error_page 404 404.html; error_page 500 502 503 504 50x.html; } } 

任何帮助非常感谢!

在你的问题中,你没有提到这一点,但是我有很好的权威,你正在Nginx上运行一个共享主机上的另一个Nginx。 ;)

当我写这篇文章的时候,Nginx的gzip模块默认使用HTTP 1.1,但Nginx在与后端服务器通信时只能使用HTTP 1.0,所以解决办法是在你的nginx.conf设置gzip_http_version ,如下所示:

 gzip_http_version 1.0; 

做出改变后重新启动你的Nginx,你应该做生意。