加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx反向代理:如何将“accept-encoding”头传递给后端服务器

发布时间:2020-12-13 21:12:53 所属栏目:Nginx 来源:网络整理
导读:在IIS 8.5前面使用nginx. IIS 8.5已配置压缩并且运行良好. 但是当我们通过nginx(下面的配置)点击它时,gzip会丢失(从Web浏览器的角度来看). 第1部分:解决方案尝试#1是在nginx上启用gzip,从浏览器的角度来看gzip,但现在(我们担心)有(a)强加双gzip开销(在iis中

在IIS 8.5前面使用nginx.

IIS 8.5已配置压缩并且运行良好.

但是当我们通过nginx(下面的配置)点击它时,gzip会丢失(从Web浏览器的角度来看).

第1部分:解决方案尝试#1是在nginx上启用gzip,从浏览器的角度来看gzip,但现在(我们担心)有(a)强加双gzip开销(在iis中使用gzip,在nginx上解压缩,重新gzip)在nginx上);或者,(b)gzip现在在nginx上,这是不理想的(因为我们可以更好地控制iis,并且iis可以更好地决定什么是静态的,什么是动态的,因此缓存更好等等).解#1的配置可以在这里看到:nginx: gzip on server is lost during proxy

Addendum 1: Per nginx docs
(07001):
” NGINX … does not “double compress” responses that are already
compressed”

This is good,in that double-zipping will not occur.

第2部分:所以我们真正想要的是接受编码头传递,从浏览器到nginx到iis,让iis做所有的压缩,并通过nginx传递gzip响应,而不会在nginx上发生任何gzip开销.我们的运行配置如下.

问题:我们如何在nginx 1.7.9中实现第2部分?

运行反向代理配置,它会删除所有gzip(例如它似乎剥离了accept-encoding标头):

http {

upstream sandbox_site {
    least_conn;
    # we pipe to back end on port 80 only,so that nginx handles all ssl
    server 192.168.2.16:80 max_fails=1 fail_timeout=60s;  # sbox3-site is .2.16
}

server {
    # This is sandbox.myapp.com block **************************************
    listen 192.168.2.27:80;
    server_name sandbox.myapp.com;

    location / {
    proxy_pass http://sandbox_site;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    # This is SSL version of sandbox.myapp.com block **************************************
    listen 192.168.2.27:443 ssl;
    server_name sandbox.myapp.com;

    ssl_certificate      new-sandbox-myapp-com.cer;
    ssl_certificate_key  new-sandbox-myapp-com.key;

    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://sandbox_site;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            
    }
} 
最佳答案
肯定主题入门者已经找到答案,但我会为了其他观众发布它们.

第一个解决方案(在nginx中启用gzip)不会导致重复压缩. Nginx非常聪明,可以看到上游已经进行了压缩而没有尝试压缩两次.

第2部分解决方案仅在代理配置中错过了一位,即:

proxy_http_version 1.1;

添加此位后,一切都将按预期工作(nginx将从上游提供gzip压缩内容)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读