[英]How to enable gzip on a Nginx web server?
我试图在我的网站上启用gzip,但效果不佳。 使用http://checkgzipcompression.com/进行检查gzip显示已启用,但是当我转到https://gtmetrix.com/来测试我的网站的性能和速度时,似乎某些文件(例如JavaScript文件和SVG)未启用gzip文件)。
我究竟做错了什么?
为了启用gzip,我使用了.htaccess并粘贴了以下代码:
<IfModule mod_mime.c>
AddEncoding gzip svgz
</IfModule>
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
在mod_deflate.c之前,我也尝试了以下代码:
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_include handler ^cgi-script$
</ifModule>
服务器信息
server nginx
vary Accept-Encoding
NGINX不支持.htaccess文件。
你做不到 你不应该 如果需要.htaccess,则可能做错了。
为了在NGINX Web服务器上启用Gzip压缩,请首先打开NGINX的默认配置文件: sudo vim /etc/nginx/nginx.conf
,然后用以下内容替换现有的Gzip设置:
nginx.conf (您可以根据需要修改以下设置)
# Enable Gzip
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;
重新启动NGINX
service nginx restart
或 /etc/init.d/nginx restart
NGINX Gzip文档: http ://nginx.org/en/docs/http/ngx_http_gzip_module.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.