Gzip compression with nginx
Solution 1
curl
doesn't Accept-Encoding: gzip
by default. You will need to either use -H "Accept-Encoding: gzip,deflate"
to get curl to request gzip or better yet, use --compressed
so curl will know to decompress the result.
Solution 2
Pictures (jpg/gif etc) are already compressed. So you don't need to (and shouldn't try to) compress them on the web server.
Here is an example of what I compress:
gzip_types text/html text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp;
Related videos on Youtube
Katta Sivasai
Updated on September 18, 2022Comments
-
Katta Sivasai over 1 year
In my nginx.conf I have:
gzip on; gzip_static on; gzip_buffers 16 8k; gzip_comp_level 9; gzip_http_version 1.0; gzip_min_length 1000; gzip_types text/plain text/css image/x-icon image/bmp image/png image/gif image/jpeg image/jpg application/json application/x-javascript text/javascript; gzip_vary on; gzip_proxied any;
So, if I fetch the headers of a picture on my server:
spiroo@glamdring:~$ curl -I http://static.mysite.com/g/pics/big_6e1855d844ebca560379139e75942f669655f.jpeg HTTP/1.1 200 OK Server: nginx Date: Thu, 04 Apr 2013 13:00:20 GMT Content-Type: image/jpeg Content-Length: 5336 Last-Modified: Mon, 25 Mar 2013 13:28:02 GMT Expires: Fri, 04 Apr 2014 13:00:20 GMT Cache-Control: max-age=31536000 Pragma: public Cache-Control: public, must-revalidate, proxy-revalidate Accept-Ranges: bytes
But if I turn off the gzip compression in nginx.conf, I have exactly the same result on the Content-Length.
What am I doing wrong?
Thanks in advance.
PS: Just to be clear, I have the nginx last release, and I am behind a proxy (haproxy).
EDIT ==>
I have the same problem with the CSS. I understand jpeg are already compressed. Of crouse, when I switch gzip on/off I restart nginx.
This is my fetch from the headers of a css file. I have the same Content-Lenght with or without gzip compression.
spiroo@glamdring:~$ curl -I http://static.mysite.com/css/9a7f503b.css HTTP/1.1 200 OK Server: nginx Date: Thu, 04 Apr 2013 14:21:50 GMT Content-Type: text/css Content-Length: 203088 Last-Modified: Tue, 02 Apr 2013 11:34:39 GMT Vary: Accept-Encoding Expires: Fri, 04 Apr 2014 14:21:50 GMT Cache-Control: max-age=31536000 Pragma: public Cache-Control: public, must-revalidate, proxy-revalidate Accept-Ranges: bytes
And this is the nginx configuration for my statics files:
server { server_name static.mysite.com; root /home/www/mysite/current/web; location / { return 404; } location ~ \.(?:jpg|jpeg|js|css|gif|png|swf|ico|pdf)$ { expires 365d; access_log off; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; }
}
-
Katta Sivasai about 11 yearsThanks for your comments. But I have the same problem with CSS if I turn on the gzip compression or if I turn off the gzip compression, the Content-Length is the same.
-
symcbean about 11 yearsCan you update your question with a more sensible confugration and relevant headers then? (BTW did you restart the server to make sure the changes were applied)
-
Drew Khoury about 11 yearsYou will need to reload or restart nginx so that it will see the new config (as symcbean said).
-
Katta Sivasai about 11 yearsQuestion updated.
-
Drew Khoury about 11 yearsHave you tried in a browser, like Chrome ... instead of just curl?
-
ericek111 over 3 yearsYou should use
application/javascript
istead ofapplication/x-javascript
: stackoverflow.com/a/9664327/3303059