Enable ETAG and Gzip with Nginx reverse proxy

The Problem

Although it takes just an option to enable gzip in your application. It sucks the energy out of your CPU. Thus, your best bet is Nginx.

But Nginx doesn’t have ETAG configuration for reverse proxy – it does have for static files. etag doc

This doesn’t end here – If you have Strong ETAG, it will not be passed when gzip is enabled.

Solution

Use weak ETAG instead of strong. While strong ETAG is removed from header by Nginx, weak etag is allowed to pass through.

Example

Example uses the iris framework written in go.


app.Get("/Cache/{key:string}", func(ctx iris.Context) {
  ...
  key := ctx.Params().Get("key")
  ...
  cached, err := readFrontCache(client, key)
  ...
  eTag :=generateEtag(cached);
  ...

  // Trick is to use weak tag
  eTag = "W/\"" + eTag + "\""

  none := ctx.GetHeader("If-None-Match")

  if none == eTag {
    ctx.WriteNotModified()
  } else {
    ctx.StatusCode(iris.StatusOK)
  }
  ctx.Header(context.ETagHeaderKey, eTag)
  ctx.JSON(raw)
})


Tell your network!

4 Comments

  1. USE MODIFIED TIME and save yourself all the trouble of ETAG. I know in some cases your CDN will be stupid enough to ignore last modified time but with brand names out there, it will not be a problem like fastcdn

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>