<p>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.</p>

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

<p>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.</p>

Jonatas Oliveira
<p>Focusing specifically on the title of this question and extending on the example of such setup as pointed by user @Raunak Jhawar.</p>
<p>You can configure your <code>nginx-ingress</code> to compress the data by updating the <code>ingress-nginx-controller</code> <strong>configmap</strong>.</p>
<p>This would work on the path:</p>
<ul>
<li><code>Pod</code> ----&gt; <code>NGINX Ingress controller</code> - <strong><code>GZIP</code></strong> -&gt; <code>Client</code> (Web browser)</li>
</ul>
<p>To enable such setup you will need to edit the <code>Ingress</code> controller configmap like below:</p>
<ul>
<li><code>$ kubectl edit configmap -n ingress-nginx ingress-nginx-controller</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>data: # ADD IF NOT PRESENT
  use-gzip: &quot;true&quot; # ENABLE GZIP COMPRESSION
  gzip-types: &quot;*&quot; # SPECIFY MIME TYPES TO COMPRESS (&quot;*&quot; FOR ALL) 
</code></pre>
<p>You can find more reference and options to configure by following below link:</p>
<ul>
<li><em><a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/" rel="noreferrer">Kubernetes.github.io: Ingress NGINX: User guide: Nginx-configuration: Configmap</a></em></li>
</ul>
<blockquote>
<p><strong>A side note!</strong></p>
<p>You can also use other methods of editing resources like: <code>$ kubectl patch</code></p>
</blockquote>
<p>This changes would make the <code>nginx-ingress-controller</code> Pod to be automatically reconfigured.</p>
<p>I've included an example of such setup below.</p>
<hr />
<p>To check if the compression occurs and if it's working I've used following setup:</p>
<ul>
<li><code>NGINX Ingress Controller</code> spawned by:
<ul>
<li><em><a href="https://kubernetes.github.io/ingress-nginx/deploy/#gce-gke" rel="noreferrer">Kubernetes.github.io: Ingress NGINX: Deploy: GCE-GKE</a></em></li>
</ul>
</li>
<li><code>NGINX</code> pod with a <code>5mb.txt</code> file filled with <strong><code>0</code></strong>'s</li>
<li><code>Service</code> and <code>Ingress</code> resource that will expose the <code>NGINX</code> pod with <code>NGINX Ingress Controller</code></li>
</ul>
<p>You can check if your setup with <code>nginx-ingress</code> supoorts <code>gzip</code> compression by either:</p>
<ul>
<li><p>Checking with <code>Developer tools</code> with a browser of your choosing:</p>
<ul>
<li><code>Chrome</code> -&gt; <code>F12</code> -&gt; <code>Network</code> -&gt; Go to site (or refresh) and press on example file (look on <code>Response Header</code>):</li>
</ul>
<p><a href="https://i.stack.imgur.com/KtWUq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KtWUq.png" alt="RESPONSE HEADER" /></a></p>
</li>
<li><p>You can also use <code>curl</code> command like below (<a href="https://stackoverflow.com/a/9140223/12257134">source</a>):</p>
<ul>
<li><code>$ curl $URL$ --silent --write-out &quot;%{size_download}\n&quot; --output /dev/null</code> - get size <strong>without</strong> compression</li>
<li><code>$ curl $URL$ --silent -H &quot;Accept-Encoding: gzip,deflate&quot; --write-out &quot;%{size_download}\n&quot; --output /dev/null</code> - get the size <strong>with</strong> compression (if supported)</li>
</ul>
</li>
</ul>
<p>Above methods have shown the compression rate of about <code>99%</code> (<code>5MB</code> file compressed to <code>50KB</code>)</p>
<hr />
<p>I also encourage you to check below links for additional reference:</p>
<ul>
<li><em><a href="https://stackoverflow.com/a/9140223/12257134">Stackoverflow.com: How can I tell if my server is serving GZipped content?
</a></em></li>
<li><em><a href="https://serverfault.com/questions/496098/gzip-compression-with-nginx">Serverfault.com: Gzip compression with nginx
</a></em> - link about images (I have learned the hard way)</li>
</ul>
