7

I'm trying to configure gzip to work in a python application that runs on a kubernetes with nginx-ingress in GKE. But I discovered that it is no use to enable gzip in the ingress-controller config-map because I need to enable compression on the backend as I understand it.

How can I enable compression on the backend of my python application to run gzip on nginx controller?

My main problem is that searching here in stackoverflow I know I need to put the compression in the backend, just do not know how to do this.

  • What's not clear is what data you want to compress the packets which server sends to clients or content within the pods? – Raunak Jhawar Jan 22 '19 at 19:46
  • This is previously discussed at https://stackoverflow.com/questions/48039952/does-gke-ingress-gce-support-compression – ahmet alp balkan Jan 23 '19 at 01:44
  • Possible duplicate of [Does GKE Ingress-GCE support compression?](https://stackoverflow.com/questions/48039952/does-gke-ingress-gce-support-compression) – ahmet alp balkan Jan 23 '19 at 01:45
  • @RaunakJhawar I want to compress the packets which server sends to clients. – Jonatas Oliveira Jan 23 '19 at 12:32
  • @AhmetB-Google So this discussion is what has to be done, my point is that I do not understand how it should be done. I did not find any documentation on how to set up a backend with compressed content. I found some libs for my application to already send compressed, but it would be a problem to apply why I would need to put a lib in all my classes and that application is still a legacy monolithic. – Jonatas Oliveira Jan 23 '19 at 12:34
  • @JonatasOliveira have you managed to solve your question? As you said that you want to configure compression from `server` -> `clients` by server you mean your `python app`? Could you share the [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your `app` and your `nginx-ingress` version for a reproduction purposes? – Dawid Kruk Dec 07 '20 at 21:16

2 Answers2

11

Focusing specifically on the title of this question and extending on the example of such setup as pointed by user @Raunak Jhawar.

You can configure your nginx-ingress to compress the data by updating the ingress-nginx-controller configmap.

This would work on the path:

  • Pod ----> NGINX Ingress controller - GZIP -> Client (Web browser)

To enable such setup you will need to edit the Ingress controller configmap like below:

  • $ kubectl edit configmap -n ingress-nginx ingress-nginx-controller
data: # ADD IF NOT PRESENT
  use-gzip: "true" # ENABLE GZIP COMPRESSION
  gzip-types: "*" # SPECIFY MIME TYPES TO COMPRESS ("*" FOR ALL) 

You can find more reference and options to configure by following below link:

A side note!

You can also use other methods of editing resources like: $ kubectl patch

This changes would make the nginx-ingress-controller Pod to be automatically reconfigured.

I've included an example of such setup below.


To check if the compression occurs and if it's working I've used following setup:

You can check if your setup with nginx-ingress supoorts gzip compression by either:

  • Checking with Developer tools with a browser of your choosing:

    • Chrome -> F12 -> Network -> Go to site (or refresh) and press on example file (look on Response Header):

    RESPONSE HEADER

  • You can also use curl command like below (source):

    • $ curl $URL$ --silent --write-out "%{size_download}\n" --output /dev/null - get size without compression
    • $ curl $URL$ --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null - get the size with compression (if supported)

Above methods have shown the compression rate of about 99% (5MB file compressed to 50KB)


I also encourage you to check below links for additional reference:

Dawid Kruk
  • 8,982
  • 2
  • 22
  • 45
3

This makes this as a nginx conversation. There are a lot of docs available which guides on hows and whats with nginx compression. Essentially, you have to add a bunch of gzip related settings and options in your nginx configuration file.

The tread shared above is a good starter and shows exactly what needs to be on the nginx conf. Please note that, as a good practice, do not compress any/all inbound packets as this essentially adds more compute burden on the CPU. The nginx conf has one option to only compress packets which exceed certain size.

Raunak Jhawar
  • 1,541
  • 1
  • 12
  • 21
  • Hi, this is correct and I believe you have also restarted your nginx service. – Raunak Jhawar Jan 23 '19 at 13:25
  • Hello @Raunak Jhawar I'm using the gzip config in my nginx-controller config map: ` gzip on; gzip_comp_level 5; gzip_http_version 1.1; gzip_min_length 256; gzip_types application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component; gzip_proxied any; gzip_vary on; ` – Jonatas Oliveira Jan 23 '19 at 13:25
  • I'm recreate my pod, but the header not found gzip. In the link https://stackoverflow.com/questions/48039952/does-gke-ingress-gce-support-compression talk about compress backend to found gzip in GKE. – Jonatas Oliveira Jan 23 '19 at 13:27