Nginx enable gzip
我想在我的Nginx服务器上启用gzip压缩。 nginx.conf文件在这里:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | http { # Enable Gzip server { location ~* \\.(?:ico|woff|css|js|gif|jpe?g|png)$ { expires 30d; add_header Pragma public; add_header Cache-Control"public"; } location /api { try_files $uri $uri/ /api/index.php; } location / { ##merge gzip on; gzip_http_version 1.0; gzip_comp_level 2; gzip_min_length 1100; gzip_buffers 4 8k; gzip_proxied any; gzip_types # text/html is always compressed by HttpGzipModule text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml; gzip_static on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\\."; gzip_vary on; try_files $uri $uri/ /index.php?q=$uri&$args; } location ~"\\.pagespeed\\.([a-z]\\.)?[a-z]{2}\\.[^.]{10}\\.[^.]+" { add_header""""; } location ~"^/ngx_pagespeed_static/" { } location ~"^/ngx_pagespeed_beacon" { } } } |
很遗憾,gzip压缩无法正常工作,Google Pagespeed和Gtmetrix无法检测到该问题。
我可以在哪里放置gzip conf?
在
我已经在
您可以将gzip配置放在任何地方,但是如果要将其应用于所有网站/文件,最好将其放在http部分中-这将是所有服务器和位置块的默认设置。我还将"缩短" /将您的配置更改为以下内容:
1 2 3 4 5 6 7 8 9 10 11 | http { gzip on; gzip_min_length 500; gzip_proxied any; gzip_comp_level 4; gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml; gzip_vary on; gzip_disable "msie6"; ... here come your server blocks / rest of your config } |
我使用该配置,它对我来说很好用-您还可以先在浏览器中测试它(例如,使用Firebug),然后再使用外部服务对其进行测试。
仅当您实际为Nginx生成gzip压缩文件(作为文件名.gz)时,才使用gzip_static,因此与启用gzip无关,这仅是第二步。