user3652565
user3652565

Reputation: 155

Configuring gzip with nginx

I am using nginx for the first time and have some confusions regarding configurations. I have a nginx as load balancer and backends as nginx as well. With my understanding I have configured mod_security module on the load balancer as its the entry point. I have also added required response headers on the load balancer. Now I have to enable the gzip for nginx. Confusion is where it should be configured? Load balancer or the backend nginx servers?

Upvotes: 1

Views: 1318

Answers (2)

Danila Vershinin
Danila Vershinin

Reputation: 9845

It depends.

For dynamic gzipping (e.g. HTML output of your site/app)

If your load balancer is a powerful machine, then you may want to do gzip on the load balancer, in order to reduce CPU usage elsewhere.

If you have some modsecurity rules that require inspecting the response body, and gzipping is done in the nodes, then that would mean that modsecurity needs to ungzip backend response/inspect/re-gzip (and thus cause processing overhead) or those rules would simply not work. That's another case when you want to gzip in load balancer.

In all other cases, I assume gzipping on the nodes would be better.

For static files

.. it's best to rely on static gzip (pre-compress your assets). However, since you have many backends, it means pre-compressing assets on each.

If your backends are different websites/apps (that means, you're not doing actual load balancing), it's not an issue.

If your backends are actual nodes of the same app, then you can do max gzip on each node, and "proxy cache" results on the load balancer.

Upvotes: 0

tom
tom

Reputation: 10539

You can configure gzip globally in /etc/nginx/nginx.conf or just for one site in e.g. /etc/nginx/sites-available/your-site.

The configuration could like this:

gzip on;

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Upvotes: 1

Related Questions