nginx - 如何关闭内网 IP 的 gzip 内容编码?

标签 nginx

我想为来自网络外部的请求启用 GZIP。

我在 nginx.conf 中尝试了下面的配置,但我得到了“gzip”指令在/etc/nginx/nginx.conf:64 中不允许的错误

nginx.conf

http {
    geo $no_gzip_list {
       default         0;
       10.0.0.0/8      1;
       172.16.0.0/12   1;
       192.168.0.0/16  1;
    }
    gzip on;

    server {
        if ($no_gzip_list = 0) {
            gzip off; #<--Line 64
        }
    }
}

我做错了什么?或者如何为外部请求启用 gzip 并为内部请求禁用 gzip?

最佳答案

官方文档说您可以在“http,服务器,位置,如果在位置”中使用gzip。

Syntax: gzip on | off;
Default:    
gzip off;
Context:    http, server, location, if in location

这意味着您必须将其移动到位置 block 内。

请看下面的例子:

http {

    geo $no_gzip_list {
       default         0;
       10.0.0.0/8      1;
       172.16.0.0/12   1;
       192.168.0.0/16  1;
    }

    gzip on;

    server {
        location / {
            ... # YOUR CODE

            if ($no_gzip_list) {
                gzip off;
            }
        }
    }
}

希望对你有帮助!

关于nginx - 如何关闭内网 IP 的 gzip 内容编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747454/

相关文章:

docker 通过 https 提供内容而没有域(仅具有公共(public) IP 地址)

javascript - Console.log 在 Node.js 中给出错误

javascript - Angular - UI 路由器路由 - HTML5 模式

php - 如何在 Ubuntu 上安装这些 PHP 扩展?

html - 带有 css 和图像文件的 nginx 自定义错误页面 502

php - Nginx 位置覆盖 Laravel 5.2 路由?

node.js - Nginx.conf 和 Node.js 设置场景

node.js - Socket.IO - io 未定义(在不同域上失败)

php - file_get_contents 无法打开流 : Cannot assign requested address

nginx "server_tokens"配置被忽略