2

Pretty straight forward question:

What are the pros/cons for enabling gzip on Spring server v.s. nginx? The Spring Server is only used to serve RESTful api requests (json) and Nginx is used as a reverse proxy before the Spring server

I searched around but no one seems have a very good answer. This answer (Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful) talks about it should be enabled on nginx because it's more efficient (and IRL people do this)

Thus I'm wondering if anyone has experience with this? What people usually do?

Eddie Xie
  • 875
  • 1
  • 8
  • 20

2 Answers2

2

If you care about performance: Dont use Nginx for that!

Nginx gzip process is a content filter, this is not compatible with send_file optimisation (more details at https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/#enabling-sendfile):

Enabling the sendfile directive eliminates the step of copying the data into the buffer and enables direct copying data from one file descriptor to another.

As such, I would recommend to use a HTTP proxy for that, (you could use another Nginx! What I am doing all the time), and setup proxy_cache:

Nginx -- (proxy cache) --> Nginx -- gzip --> CSS/JS files

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
1

Most people would recommend enabling it on nginx. The thinking is to free Spring from as much work as possible since:

  • Spring uses considerably more memory per request and connection than nginx.
  • Spring might hold on to a database connection while it's processing a request. And database connections on the database server are expensive too.
  • Spring is more difficult and more expensive to scale up than nginx if the load requires so.

For most setups, the difference will be small and not noticeable. I have never measured it in practice. Other people might have more experience.

Codo
  • 75,595
  • 17
  • 168
  • 206