Nginx 反向代理 Gzip 到客户端

Dea*_*mas 10 nginx reverse-proxy

我有一个 IIS 6.0 Web 服务器暴露在互联网上,它不能可靠地执行 Gzipping。我知道它已经过时了,但这就是我必须为这个实例工作的全部内容。

我想要做的是将 Nginx(或类似的)作为反向代理和缓存服务器放在 IIS 前面,以加快网站速度。但是,我不确定是否可以将 nginx 服务器从自身 gzip 压缩到 Web 浏览器。如果 IIS 将 Gzipped 请求传递回 nginx,它们就会很好地返回到浏览器。

gzip                on;
gzip_min_length     1000;
gzip_buffers        4 8k;
gzip_http_version   1.0;
gzip_disable        "msie6";
gzip_types          text/plain text/css;
gzip_vary           on;

location / {
    proxy_set_header x-real-IP $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
    proxy_set_header host $host;
    proxy_pass http://192.168.5.37;
}
Run Code Online (Sandbox Code Playgroud)

HTTP 请求 (/css/components.css)

GET /css/components.css HTTP/1.1
Host: www.mydomain.co.uk
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/css,*/*;q=0.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Run Code Online (Sandbox Code Playgroud)

HTTP 响应 (/css/components.css)

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 22 Oct 2015 14:26:08 GMT
Content-Type: text/css
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Last-Modified: Sat, 22 Aug 2015 09:36:54 GMT
ETag: W/"07f8614bedcd01:8beb"
X-Powered-By: ASP.NET
Run Code Online (Sandbox Code Playgroud)

是否有我在某处丢失的魔术参数来告诉它压缩 nginx 中的文件?

谢谢!

院长

Pie*_*RET 11

您可以将该gzip_proxied any;指令添加到您的 conf 中。

编辑:

我的测试:
我在我的机器 (192.168.122.1) 上使用python -m http.server 8080. 当我请求它时,我得到:

[pat@morbier ~]$ curl -I -H 'Accept-Encoding: gzip,deflate' http://192.168.122.1:8080/
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.5.0
Date: Thu, 22 Oct 2015 17:41:39 GMT
Content-type: text/html; charset=utf-8
Content-Length: 1197
Run Code Online (Sandbox Code Playgroud)

我用nginx(192.168.122.224)设置了一个代理

gzip_min_length     1000;
gzip_buffers        4 8k;
gzip_http_version   1.0;
gzip_disable        "msie6";
gzip_types          text/plain text/css;
gzip_vary           on;

location / {
    proxy_set_header x-real-IP $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
    proxy_set_header host $host;
    proxy_pass http://192.168.122.1:8080;
}
Run Code Online (Sandbox Code Playgroud)

当我向代理请求同样的事情时,我得到

[pat@morbier ~]$ curl -I -H 'Accept-Encoding: gzip,deflate http://192.168.122.224/
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 22 Oct 2015 17:46:08 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 1197
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

然后我添加到代理conf

gzip on;
gzip_proxied any;
Run Code Online (Sandbox Code Playgroud)

我得到

[pat@morbier ~]$ curl -I -H 'Accept-Encoding: gzip,deflate' http://192.168.122.224/
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 22 Oct 2015 17:47:54 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Content-Encoding: gzip
Run Code Online (Sandbox Code Playgroud)

如果不使用 -I 卷曲,您也可以看到它(因此获得整个页面),在我的情况下,它变成了终端上显示的一些二进制文件,加强了它被压缩的事实。当我卷曲没有 gzip 的常规网站时,我得到了网页的内容 (HTML)。

edit2:
它实际上只适用于您启用的选项,似乎不需要 gzip_proxied。
啊,您在请求时没有传递“接受编码:gzip,deflate”标头(我猜您正在使用 telnet)。在这种情况下它不起作用,因为您没有告诉服务器您可以处理 gzip 编码的二进制文件。
请尝试使用 curl。