How to enable gzip on proxy servers on nginx
I use often Gunicorn as web server for django applications.
django (https://www.djangoproject.com)
Usually I use Apache but I’m starting to use nginx as webserver to serve both the static files and the proxied gunicorn response.
I need to do something like what I’ve done with Apache to compress the response after I received from django since I’ve noticed that in my case compressing it before using @gzip_page decorator is more detrimental to performance than doing it after.
@gzip_page decorator (https://docs.djangoproject.com)
Here an essential mysite.conf to put in /etc/nginx/conf.d.
<br> server {<br> listen 80;<br> server_name .example.com other.domain.example.com;<br> charset utf-8;<br> # max upload size<br> client_max_body_size 75M;<br> # Enable gzip for proxied requests and static files<br> gzip on;<br> gzip_proxied any;<br> gzip_vary on;<br> gzip_http_version 1;<br> gzip_types application/javascript application/json text/css text/xml;<br> gzip_comp_level 4;<br> # Serve static files via nginx<br> location /media {<br> alias /usr/local/etc/files/mysite/media_root;<br> }<br> location /static {<br> alias /usr/local/etc/files/mysite/static_root;<br> }<br> location / {<br> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br> proxy_set_header Host $http_host;<br> proxy_redirect off;<br> # Serve dynamic requests via gunicorn on custom port (e.g. 8585)<br> # and gzip the response<br> if (!-f $request_filename) {<br> proxy_pass http://localhost:8585;<br> break;<br> }<br> }<br> }<br>
In this way, content by Gunicorn is served to nginx and before to send it to client nginx gzip it, here with a compression level of 4 of A compression between 1 and 4 is generally acceptable for any text content, avoiding to stress the CPU too much for a small compression gain.
Even the static files like css and javascript are served compressed for client with HTTP 1 support. You can add more type to compress including the mime type on the gzip_types row.
Read also on the same topic: How to enable gzip on proxy servers on Apache
Reference:
*
*
https://web.archive.org/web/20150918000000*/https://www.djangoproject.com/ (https://web.archive.org)
https://web.archive.org/web/20150918000000*/https://chirale.wordpress.com/2013/07/15/how-to-enable-gzip-on-proxy-servers-on-apache/ (https://web.archive.org)
https://web.archive.org/web/20150918000000*/https://docs.djangoproject.com/en/1.8/topics/http/decorators/#module-django.views.decorators.gzip (https://web.archive.org)
break;<br> }<br> }<br> }<br> </pre></p> <p>In this way, content by Gunicorn is served to nginx and before to send it to client nginx gzip it, here with a compression level of 4 of 9. A compression between 1 and 4 is generally acceptable for any text content, avoiding to stress the CPU too much for a <a href= (https://web.archive.org)
https://web.archive.org/web/20150918000000*/https://serverfault.com/a/452642/129620 (https://web.archive.org)
https://web.archive.org/web/20150918000000*/http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/ (https://web.archive.org)
https://web.archive.org/web/20150918000000*/http://nginx.org/en/docs/http/ngx_http_gzip_module.html (https://web.archive.org)
Response: 20 (Success), text/gemini
| Original URL | gemini://chirale.org/2015-09-18_1291.gmi |
|---|---|
| Status Code | 20 (Success) |
| Content-Type | text/gemini; charset=utf-8 |