【问题标题】:Disable gzip compression only for https connections on nginx仅对 nginx 上的 https 连接禁用 gzip 压缩
【发布时间】:2018-02-03 06:33:42
【问题描述】:

拥有一个应该为 http 和 https 提供内容的服务器块:

server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  include snippets/certificate.conf;
  include snippets/ssl-params.conf;
  root ...
}

是否可以只为同一服务器块中的 https 连接配置 gzip 压缩,还是我必须放弃它们?

编辑:

实际上可以检查位置块内的请求方案,如果等于 https,则将 gzip 设置为关闭:

server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  include snippets/certificate.conf;
  include snippets/ssl-params.conf;

  location / {
      if ($scheme = "https") {
          gzip off;
      }
      try_files $uri $uri/ =404;
  }

  ...
}

问题是在 if 块 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

中只使用 rewritereturn 语句似乎是安全的

【问题讨论】:

    标签: nginx https server


    【解决方案1】:

    这是不可能的

    语法:gzip on |关闭;

    默认值:

    gzip 关闭;

    上下文:http、服务器、位置,如果在位置中

    如您所见,它只能在这些块中使用http, server, location, if in location。而且它不允许值的任何参数

    nginx: [emerg] invalid value "$gzip_flag" in "gzip" directive, it must be "on" or "off" in /usr/local/openresty/nginx/conf/nginx.conf:15
    

    因此,您必须将服务器分成两部分。但是因为你的其他东西都是通用的,你可以把所有的东西都放在一个包含文件中。在两个服务器位置都包含该文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多