# Weak ETags and on-the-fly gzipping

**URL:** <https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590>\
**Category:** NGINX\
**Created:** [June 15, 2013, 2:59pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590 "2013-06-15T14:59:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [June 15, 2013, 2:59pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/1 "2013-06-15T14:59:12Z")

</div>

I am serving dynamic requests behind the Nginx HTTP server. HTTP  
requests  
are mostly from mobile HTTP clients. That’s is why I care about two  
things.

1. Do not send the same representation twice: Use ETag caching  
mechanism.
2. Make better us of available bandwidth: Use Accept-Encoding HTTP  
compression.

Today I noticed that these two don’t match. My application sets the ETag  
header in the response, but when I set the ‘Accept-Encoding: gzip’ in  
the  
request header, nginx clears the ETag header when gzipping on-the-fly.

I understand why this is: If two responses have the same ETag, their  
bodies  
should be byte-for-byte comparable. When the content is gzipped, and  
actually modified, this is of course not the case. The gzipped response  
is  
not equal to the non-gzipped response.

That’s why there are two ETag validation mechanisms. A strong  
validation,  
used in case of byte-for-byte comparable responses, and a weak  
validation,  
to indicate semantic equivalency.

In the case of weak validation an ETag would look like:  
ETag: W/“123456789”

Why is Nginx stripping weak ETag validators in its gzip filter?

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [June 16, 2013, 4:08am UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/2 "2013-06-16T04:08:58Z")

</div>

Hello!

On Sat, Jun 15, 2013 at 02:58:33PM +0200, Matthijs L. wrote:

> ETag: W/“123456789”
> 
> Why is Nginx stripping weak ETag validators in its gzip filter?

Just stripping ETags are easier than converting them to weak  
etags (and implementing weak etags support various places).

On the other hand, relevant caching functionality is still here  
with Last-Modified cache validator.

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [June 16, 2013, 11:40am UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/3 "2013-06-16T11:40:30Z")

</div>

Hi, thanks for answering so quickly.

You are right, we should not go so far as to implement a conversion from  
strong to weak ETags.  
And alo, Last-Modified header could be preferable.

Wen dynamic content is generated, setting a Last-Modified header  
requires  
inside knowledge from the app server. ETags are much simpler, as it’s  
often  
implemented as an md5 hash of the response body. A (Ruby) app server can  
easily add an ETag to all responses. Setting a Last-Modified header is  
different per request, depending on what dynamic resource is generated.

So I would not say that the Last-Modified header covers all cases that  
ETag  
does.

Instead of stripping all ETags, I would propose to only strip strong  
ETags.

According to the spec:  
[HTTP/1.1: Caching in HTTP](http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.3) a weak  
ETag is perfectly fine for validating semantic equivalence. Just not for  
range requests.

So by adding something like:

if (is\_strong\_etag(r)) {  
ngx\_http\_clear\_etag(r)  
}

We would allow applications to set weak Etags for caching.

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [October 18, 2013, 11:13pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/4 "2013-10-18T23:13:21Z")

</div>

Please someone implement weak etags or a “do\_not\_strip\_etags” options  
for  
this,  
it’s a big hit on our performance / page response time and we cannot  
simply  
replace that with Last-Modified.

Posted at Nginx Forum:

> **[Re: Weak ETags and on-the-fly gzipping](https://forum.nginx.org/read.php?2,240120,243845#msg-243845)**
>
> I am serving dynamic requests behind the Nginx HTTP server. HTTP requests are mostly from mobile HTTP clients. That's is why I care about two things. 1. Do not send the same representation twice: Use ETag caching mechanism. 2. Make better us of...

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [October 18, 2013, 11:20pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/5 "2013-10-18T23:20:22Z")

</div>

There take this patch and just apply it 🙂

— nginx-1.3.8/src/http/modules/ngx\_http\_gzip\_filter\_module.c  
2012-07-07 17:22:27.000000000 -0400  
+++  
nginx-1.3.8-weak-etags-shorter/src/http/modules/ngx\_http\_gzip\_filter\_module.c2012-11-21  
17:05:12.758389000 -0500  
@@ -306,7 +306,15 @@

ngx\_http\_clear\_content\_length(r);  
ngx\_http\_clear\_accept\_ranges(r);

- ngx\_http\_clear\_etag(r);

- 
- /\* Clear etags unless they’re marked as weak (prefixed with ‘W/’) \*/
- h = r-\>headers\_out.etag;
- if (h && !(h-\>value.len \>= 3 &&
- h-\>value.data[0] == ‘W’ &&
- h-\>value.data[1] == ‘/’ &&
- h-\>value.data[2] == ‘"’)) {
- ngx\_http\_clear\_etag(r);
- }

return ngx\_http\_next\_header\_filter(r);  
}

Posted at Nginx Forum:

> **[Re: Weak ETags and on-the-fly gzipping](https://forum.nginx.org/read.php?2,240120,243846#msg-243846)**
>
> I am serving dynamic requests behind the Nginx HTTP server. HTTP requests are mostly from mobile HTTP clients. That's is why I care about two things. 1. Do not send the same representation twice: Use ETag caching mechanism. 2. Make better us of...

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 17, 2016, 4:06pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/6 "2016-05-17T16:06:21Z")

</div>

Is there anyway i can override this from nginx config file?  
I need weak etag to be untouched by nginx ☹

Posted at Nginx Forum:

> **[Re: Weak ETags and on-the-fly gzipping](https://forum.nginx.org/read.php?2,240120,266944#msg-266944)**
>
> I am serving dynamic requests behind the Nginx HTTP server. HTTP requests are mostly from mobile HTTP clients. That's is why I care about two things. 1. Do not send the same representation twice: Use ETag caching mechanism. 2. Make better us of...

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 17, 2016, 5:00pm UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/7 "2016-05-17T17:00:41Z")

</div>

Hello!

On Tue, May 17, 2016 at 10:05:53AM -0400, neoelit wrote:

> Is there anyway i can override this from nginx config file?  
> I need weak etag to be untouched by nginx ☹

Weak ETags are supported and used since nginx 1.7.3 as released on  
08 Jul 2014, quote from [http://nginx.org/en/CHANGES:](http://nginx.org/en/CHANGES:)

```
*) Feature: weak entity tags are now preserved on response
   modifications, and strong ones are changed to weak.

```

You are writing to an ancient thread from 2013.

If you are still using an older version, it should be a good idea  
to upgrade.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 18, 2016, 6:42am UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/8 "2016-05-18T06:42:02Z")

</div>

Thanks for the quick reply. 🙂  
Yeah so nginx shouldn’t be the issue. I’m trying to figure why  
If-None-Match  
header not reaching rails on passenger-standalone. Since it has nginx  
version 1.8+ this should be the issue.  
[https://groups.google.com/forum/#!topic/phusion-passenger/eZgw3TqrfSI](https://groups.google.com/forum/#!topic/phusion-passenger/eZgw3TqrfSI)

Posted at Nginx Forum:

> **[Re: Weak ETags and on-the-fly gzipping](https://forum.nginx.org/read.php?2,240120,266970#msg-266970)**
>
> I am serving dynamic requests behind the Nginx HTTP server. HTTP requests are mostly from mobile HTTP clients. That's is why I care about two things. 1. Do not send the same representation twice: Use ETag caching mechanism. 2. Make better us of...

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 18, 2016, 6:43am UTC](https://www.ruby-forum.com/t/weak-etags-and-on-the-fly-gzipping/245590/9 "2016-05-18T06:43:47Z")

</div>

## neoelit Wrote:

> Thanks for the quick reply. 🙂  
> Yeah so nginx shouldn’t be the issue. I’m trying to figure why  
> If-None-Match header not reaching rails on passenger-standalone. Since  
> it has nginx version 1.8+ this should be the issue.  
> I mean this shouldn’t be the issue 😛  
> [Redirecting to Google Groups](https://groups.google.com/forum/#!topic/phusion-passenger/eZgw3TqrfSI)

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,240120,266971#msg-266971](https://forum.nginx.org/read.php?2,240120,266971#msg-266971)
