# Gziping static files

**URL:** <https://www.ruby-forum.com/t/gziping-static-files/209507>\
**Category:** NGINX\
**Created:** [July 15, 2011, 10:29am UTC](https://www.ruby-forum.com/t/gziping-static-files/209507 "2011-07-15T10:29:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 15, 2011, 10:29am UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/1 "2011-07-15T10:29:53Z")

</div>

Hi,  
I know nginx’s gzip module can compress files onthefly, and gzip\_static  
module can serve manually gziped file.

Now is there any module/way available to cache the onthefly compression  
? I dont want to compress all static files manually, i want nginx to  
compress a file first, then save it to cache, then serve the cached  
gziped version as long as the main file is not changed.

Is there any way around ?

Posted at Nginx Forum:

> **[Gziping static files](https://forum.nginx.org/read.php?2,212423,212423#msg-212423)**
>
> Hi, I know nginx's gzip module can compress files onthefly, and gzip\_static module can serve manually gziped file. Now is there any module/way available to cache the onthefly compression ? I dont want to compress all static files manually, i want...

---

<div class="post-metadata">

**Author:** ![sarim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sarim](https://www.ruby-forum.com/u/sarim)\
**Post date:** [July 26, 2011, 4:40pm UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/2 "2011-07-26T16:40:18Z")

</div>

On Fri, Jul 15, 2011 at 3:29 AM, sarim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

There are rumors of a gzip cache feature coming that I have seen here.  
For now, you can have nginx proxy to itself, with the intermediate  
doing the compression. So it would be something like this:

nginx server on port 80/443 listening on public-facing IP  
proxy\_cache enabled  
gzip enabled  
|  
V  
nginx server on localhost port 20080  
gzip enabled  
|  
V  
your back-end server

–  
RPM

---

<div class="post-metadata">

**Author:** ![sarim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sarim](https://www.ruby-forum.com/u/sarim)\
**Post date:** [July 26, 2011, 5:58pm UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/3 "2011-07-26T17:58:41Z")

</div>

> > Now is there any module/way available to cache the onthefly compression  
> > ? I dont want to compress all static files manually, i want nginx to  
> > There are rumors of a gzip cache feature coming that I have seen here.

I’m working on getting some code we wrote to be released as open source  
that  
does just that.

–  
Brian A.

---

<div class="post-metadata">

**Author:** ![sarim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sarim](https://www.ruby-forum.com/u/sarim)\
**Post date:** [August 11, 2011, 3:51pm UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/4 "2011-08-11T15:51:32Z")

</div>

## sarim Wrote:

> > gzip enabled  
> > |  
> > V  
> > your back-end server
> 
> Just tried to do that, but nginx is not caching  
> gziped files. It caches in un-gziped state, so  
> nginx gziping on every hit ☹

The middle tier needs to have gzip enabled, as well as the front-end  
tier. You will also need to enable gzip for HTTP 1.0 on the middle tier  
using “gzip\_http\_version 1.0;” This is because nginx uses HTTP 1.0 to  
talk with back-end servers (which in this case is nginx itself).  
Finally, consider normalizing the gzip headers and adding it to your  
proxy\_cache\_key at the frontmost layer, like so:

#normalize all incoming accept-encoding headers to just gzip or empty  
string  
#prevents caching of multiple versions of files based on differing  
browser accept-encoding headers  
set $myae “”; #use empty string if accept-encoding does not contain  
gzip  
if ($http\_accept\_encoding ~\* gzip) {  
set $myae “gzip”;  
}  
location {  
proxy\_pass [http://localhost:20080](http://localhost:20080); #middle tier doing compression  
proxy\_set\_header Host $host;  
proxy\_set\_header Accept-Encoding $myae;  
proxy\_cache zone1;  
proxy\_cache\_key “$request\_uri $myae”; #use normalized accept  
encoding as part of cache key  
}

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212423,213699#msg-213699](http://forum.nginx.org/read.php?2,212423,213699#msg-213699)

---

<div class="post-metadata">

**Author:** ![sarim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sarim](https://www.ruby-forum.com/u/sarim)\
**Post date:** [August 11, 2011, 11:45am UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/5 "2011-08-11T11:45:00Z")

</div>

## Ryan M. Wrote:

> your back-end server  
> Just tried to do that, but nginx is not caching gziped files. It caches  
> in un-gziped state, so nginx gziping on every hit ☹

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212423,213686#msg-213686](http://forum.nginx.org/read.php?2,212423,213686#msg-213686)

---

<div class="post-metadata">

**Author:** ![sarim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sarim](https://www.ruby-forum.com/u/sarim)\
**Post date:** [August 11, 2011, 4:03pm UTC](https://www.ruby-forum.com/t/gziping-static-files/209507/6 "2011-08-11T16:03:56Z")

</div>

gzip\_http\_version 1.0;  
Does the trick 😃 😃 😃  
Thank you very much rmalayter. 🙂

Posted at Nginx Forum:

> **[Re: Gziping static files](https://forum.nginx.org/read.php?2,212423,213700#msg-213700)**
>
> Hi, I know nginx's gzip module can compress files onthefly, and gzip\_static module can serve manually gziped file. Now is there any module/way available to cache the onthefly compression ? I dont want to compress all static files manually, i want...
