Javascript Nginx不提供gzip内容

Javascript Nginx不提供gzip内容,javascript,nginx,compression,webserver,gzip,Javascript,Nginx,Compression,Webserver,Gzip,我对nginx1.13.3(作为docker容器运行)和gzip内容有问题。服务器未发送压缩文件。数小时的搜索和反复试验并不能解决此问题: 我有一个简单的index.html,一个javascript文件只有一个脚本标记 此javascript文件已在fs上压缩(gzip:~360KB,未压缩:~1,2MB) 我所期望的是: 如果我没有进一步的nginx配置,js文件将按原样提供,并且我在浏览器中得到一个解析错误。=>这“工作”如预期 我配置了以下内容(或另一个与js文件匹配的位置块):

我对nginx1.13.3(作为docker容器运行)和gzip内容有问题。服务器未发送压缩文件。数小时的搜索和反复试验并不能解决此问题:

  • 我有一个简单的index.html,一个javascript文件只有一个脚本标记
  • 此javascript文件已在fs上压缩(gzip:~360KB,未压缩:~1,2MB)
我所期望的是:

  • 如果我没有进一步的nginx配置,js文件将按原样提供,并且我在浏览器中得到一个解析错误。=>这“工作”如预期
  • 我配置了以下内容(或另一个与js文件匹配的位置块):

  • 这将为文件提供完整的1,2MB,并且没有内容编码头。浏览器(本例中为Firefox,但也使用Chrome)发送以下请求头:

    User-Agent      "Mozilla/5.0 (Windows NT 10.0;… Gecko/20100101 Firefox/54.0"
    Accept          "*/*"
    Accept-Language "en,de;q=0.7,en-US;q=0.3"
    Accept-Encoding "gzip, deflate"
    Connection      "keep-alive"
    Pragma          "no-cache"
    Cache-Control   "no-cache"
    
    响应标题:

    Server          "nginx/1.13.3"
    Date            "Mon, 24 Jul 2017 19:35:01 GMT"
    Content-Type    "text/plain"
    Last-Modified   "Mon, 24 Jul 2017 19:06:10 GMT"
    Connection      "keep-alive"
    Etag            "\"59764522-53db0\""
    Accept-Ranges   "bytes"
    Content-Length  "1281624"
    
    但请求日志显示压缩文件的内容长度:

    GET /main.js HTTP/1.1" 200 343472
    
    我还尝试使用带有“gzip on”的即席压缩,以及带有未压缩文件的相应文件类型,但这也不起作用

    完整的nginx.conf文件:

    events {
        worker_connections 1024;
    }
    
    http {
         server {
              listen       80;
              server_name  localhost;
              # gzip off;
              #gzip_types text/plain application/javascript;
    
              location / {
                  index     index.html  index.htm;
                  root /usr/share/nginx/html;
                  add_header X-URI $uri;
    
                  if ($uri = "/main.js") {
                      add_header Content-Encoding gzip;
                  }
              }
         }
    }
    
    也许有人知道我做错了什么。以前我用nginx多次配置过这样的gzip压缩,而且一直都很有效

    谢谢

    events {
        worker_connections 1024;
    }
    
    http {
         server {
              listen       80;
              server_name  localhost;
              # gzip off;
              #gzip_types text/plain application/javascript;
    
              location / {
                  index     index.html  index.htm;
                  root /usr/share/nginx/html;
                  add_header X-URI $uri;
    
                  if ($uri = "/main.js") {
                      add_header Content-Encoding gzip;
                  }
              }
         }
    }