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.
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:
NGINX Ingress Controller
spawned by:
NGINX
pod with a 5mb.txt
file filled with 0
'sService
and Ingress
resource that will expose the NGINX
pod with NGINX Ingress Controller
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
):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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With