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)
})


Enable ETAG and Gzip with Nginx reverse proxy

2 thoughts on “Enable ETAG and Gzip with Nginx reverse proxy

Leave a Reply to M. Mante Cancel reply

Your email address will not be published. Required fields are marked *

Scroll to top