大网数据广告活动
服务器运维问答

Nginx not sending gzipped assets when passing trough a load balancer

· 52CE 我爱测

I have a nginx server acting as load balancer, which delegates requests to other application servers. When I try to request a asset directly to the application server, the asset is served in it's gzipped version, sample: ➜ ~ curl…

问题原文

原帖链接 · Iván Guillén · CC BY-SA 3.0

I have a nginx server acting as load balancer, which delegates requests to other application servers.

When I try to request a asset directly to the application server, the asset is served in it's gzipped version, sample:

➜ ~ curl -IH 'Accept-Encoding: gzip, deflate' http://application/asset.css HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Thu, 15 Sep 2016 14:13:03 GMT Content-Type: text/css Content-Length: 35038 Connection: keep-alive Content-Encoding: gzip Expires: Thu, 31 Dec 2037 23:55:55 GMT Cache-Control: max-age=315360000 Cache-Control: public

While the same request to the load balancer, returns the non-compressed version of the asset. ➜ ~ curl -IH 'Accept-Encoding: gzip, deflate' https://load-balancer/asset.css HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Thu, 15 Sep 2016 14:16:15 GMT Content-Type: text/css Content-Length: 240442 Connection: keep-alive Expires: Thu, 31 Dec 2037 23:55:55 GMT Cache-Control: max-age=315360000 Cache-Control: public Accept-Ranges: bytes

Here my configuration for the LB: location / { client_max_body_size 10M; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto https; # if use ssl proxy_redirect off; proxy_pass http://application; }

thanks in advance

社区高分解答

原帖链接 · mahyard · CC BY-SA 3.0

There is another way to solve this. You can set a higher HTTP protocol version for proxying connections to upstream. It can be done by this parameter: proxy_http_version 1.1;

It would be a better choice because this way you can benefit from lots of HTTP/1.1 advantages like persistent connections and Extra new status codes.

For example this is my own settings block:

server {
    listen       80;
    server_name  domain.tld;
    location / {
         include proxy_params;
         proxy_http_version 1.1;
         proxy_pass http://my-up-stream;
    }
}

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。