如何在Nginx网络服务器上启用gzip
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文件。
类似Apache:.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
相关文章:
- 使用JSP从服务器检索和显示图像
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 客户端服务器REST API captcha实现
- 正在将base64 jpeg从input-type=file上传到服务器
- Webpack开发服务器和React服务器端渲染
- 如何在Nginx网络服务器上启用gzip
- 根据服务器时间禁用启用单选按钮
- 使用AngularJS和外部NodeJS服务器启用html5模式
- 我必须在服务器端更改什么才能启用chunkedMode=true
- Restify静态Web服务器在启用HTTP基本身份验证后停止工作
- 在express服务器上启用HTTPS
- 什么'如果您在服务器上启用Gzip,那么缩小JavaScript/CSS源代码的意义何在
- 启用Gzip压缩托管在其他服务器上的javascript
- asp.net服务器端禁用了客户端启用的单选按钮
- 检测在服务器端启用的Javascript
- 是否可以使用服务器端代码在浏览器中启用javascript
- Rails 3 检查 Javascript 是否已启用(服务器端)
- 在tomcat服务器上启用gzip压缩可以减少js的冗余
- 如何使用jquery客户端从另一个域POST到启用Web api的服务器
- 通过服务器端禁用的控件仍然在客户端启用