简体   繁体   English

gzip_static不适用于Nginx

[英]gzip_static not working with nginx

I've got some compressed javascript files being served by nginx: 我有一些由nginx提供的压缩javascript文件:

<script type="application/javascript" src="js/shim.min.js.gz"></script>
<script type="application/javascript" src="js/zone.js.gz"></script>

but it appears that nginx is serving them as text/plain resulting in browser errors: 但似乎nginx将其作为文本/纯文本提供,导致浏览器错误:

SyntaxError: illegal character   shim.min.js.gz:1

Looking at the headers, this is the response: 查看标题,这是响应:

Content-Encoding:gzip
Content-Type:text/plain
Date:Tue, 29 Nov 2016 18:03:01 GMT
ETag:W/"583ce194-68b3"
Last-Modified:Tue, 29 Nov 2016 02:01:56 GMT 
Server:nginx/1.10.2
Vary:Accept-Encoding

Here is my nginx.conf: 这是我的nginx.conf:

worker_processes 4;

events { worker_connections 1024; }

http {
include /etc/nginx/mime.types;

gzip on;
gzip_static on;
gzip_disable "msie6";

gzip_vary on;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

        upstream node-app {
              server node1:3000 weight=10 max_fails=3 fail_timeout=30s;
        }

        server {
              listen 80;
              index index.html
              error_log  /var/log/nginx/error.log;
              access_log /var/log/nginx/access.log;
              root /var/www/public;

              location /api {
                proxy_pass http://node-app;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
              }
        }
}

I have gzip_static set to on but it doesn't appear to be working. 我已将gzip_static设置为on,但似乎无法正常工作。 I am using the nginx docker image: 我正在使用Nginx码头工人形象:

>nginx:1.10.2-alpine > nginx:1.10.2-高山

This image is compiled with the gzip static module: 该映像是使用gzip静态模块编译的:

--with-http_gzip_static_module --with-http_gzip_static_module

if I uncompress the javascripts and serve them uncompressed everything works fine. 如果我解压缩javascript并以未压缩的方式提供它们,则一切正常。 Is there an issue with the mime types? MIME类型有问题吗? This works fine with: 这适用于:

<script type="application/javascript" src="js/shim.min.js"></script>
<script type="application/javascript" src="js/zone.js"></script>

Module ngx_http_gzip_static_module : 模块ngx_http_gzip_static_module

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files. ngx_http_gzip_static_module模块允许发送扩展名为“ .gz”的预压缩文件,而不是常规文件。

gzip_static : gzip_static

Enables (“on”) or disables (“off”) checking the existence of precompressed files. 启用(“ on”)或禁用(“ off”) 检查预压缩文件的存在

We have to have two files for supported compression clients ( /some/path/js/filename.js.gz ) and not supported ( /some/path/js/filename.js ). 我们必须有两个文件用于受支持的压缩客户端( /some/path/js/filename.js.gz )和不受支持的( /some/path/js/filename.js )。

The files can be compressed using the gzip command, or any other compatible one. 可以使用gzip命令或任何其他兼容的命令压缩文件。

Use in your html: 在您的html中使用:

<script type="application/javascript" src="/js/filename.js"></script>

and nginx will return to the client one of the files. Nginx将把其中一个文件返回给客户端。

It is recommended that the modification date and time of original and compressed files be the same. 建议原始文件和压缩文件的修改日期和时间相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM