在Spring服务器上使用Gzip还是在Nginx上使用(反向代理)?

huangapple go评论63阅读模式
英文:

Gzip on Spring server or Nginx (reverse proxy)?

问题

很直接的问题:

在 Spring 服务器和 nginx 上启用 gzip,各自有什么优缺点?Spring 服务器仅用于提供 RESTful API 请求(json),而 Nginx 则用作在 Spring 服务器之前的反向代理。

我搜索了一些资料,但似乎没有人给出一个很好的答案。这个回答(链接:https://stackoverflow.com/questions/21410317/using-gzip-compression-with-spring-boot-mvc-javaconfig-with-restful)提到应该在 nginx 上启用,因为它更高效(在实际中人们会这样做)。

因此,我想知道是否有人有相关经验?人们通常是怎么做的?

英文:

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 (https://stackoverflow.com/questions/21410317/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?

答案1

得分: 2

如果您关心性能:请不要在这种情况下使用Nginx!

Nginx的gzip处理是一个内容过滤器,这与send_file优化不兼容(更多详细信息请参阅 https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/#enabling-sendfile):

> 启用sendfile指令可以消除将数据复制到缓冲区的步骤,并实现从一个文件描述符直接复制数据到另一个文件描述符。

因此,我建议为此使用HTTP代理(您可以使用另一个Nginx!这是我一直在做的),并设置proxy_cache

Nginx --(代理缓存)--> Nginx -- gzip --> CSS/JS文件

英文:

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

答案2

得分: 1

大多数人会建议在 nginx 上启用它。这样做的想法是尽量减轻 Spring 的工作负担,因为:

  • Spring 每个请求和连接使用的内存比 nginx 多得多。
  • Spring 在处理请求时可能会保持数据库连接。而数据库服务器上的数据库连接也很昂贵。
  • Spring 的扩展比 nginx 更加困难和昂贵,如果负载要求如此。

对于大多数设置,差异将会很小,不会引人注意。我从未在实践中进行过测量。其他人可能有更多经验。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月8日 15:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63788742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定