I'm currently trying to set up Nginx to serve all of my static files. Since they will not be changed very frequently at all I want to us the gzip_static module to allow me to pre-gzip copies of my files to save some cpu time and allow better compression.
I Compiled Nginx with --with-http_gzip_static_module
and set it up so that it is serving my static files, no problems so far. I wanted to test and make sure that the static gzipping was actually working, so I made two files, test.txt
and test.txt.gz
. The first line of each of the files says whether they were gzipped, and then there's a newline and 256 random characters (different betweeen the two files).
I've read that the modification time of the file and it's gzipped counterpart should be the same, and I've tried both of the following:
touch test.*
touch -r test.txt test.txt.gx
On my local machine, I'm testing with curl:
curl $URL/test.txt
This works fine, I get back the version that I didn't pre-compress, but when I do this:
curl -H "Accept-Encoding: gzip" $URL/test.txt | gunzip
I also get back the version I didn't precompress. I tried setting gzip off
in my nginx.conf
, but it doesn't make a difference. I've also re-compiled Nginx with --without-http_gzip_module
and that doesn't seem to make a difference either, Nginx still gzips things itself on the fly.
I'm quite new to Nginx, but I'm really at a loss here.
Here is the output of ./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
And here is my 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;
}
}
Any help is very appreciated!
Best Answer
You didn't mention this in your question, but I have it on good authority that you are running Nginx proxied behind another Nginx on a shared host. ;)
At the time I'm writing this, Nginx's gzip modules use HTTP 1.1 by default, but Nginx can only use HTTP 1.0 when communicating with back-end servers, so the solution is to set
gzip_http_version
in yournginx.conf
, like so:Restart your Nginx after making that change, and you should be in business.