我一直专注于优化某个网站,使其在Google PageSpeed Insights工具(针对移动设备和桌面设备)上达到100分。 大部分的项目是完美的工作,但我继续得到“启用压缩”警告的网站。
这很麻烦,因为我的服务器上启用了gzip,并且未压缩的唯一资源来自NGINX PageSpeed模块。 我已经浏览了Google网站上的configuration页面,但没有任何描述如何启用压缩function,除了已经存在的一般NGINXconfiguration之外。
我的问题是这样的:如何启用gzip压缩,使其适用于pagespeed资源?
我的服务器设置:
Ubuntu 12.0.4.3 LTS NGINX – 使用PageSpeed模块1.6.29.5testing版自定义编译1.5.4
NGINX服务器configuration:
user www-data; #set worker processes to cpu processes worker_processes 4; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { client_max_body_size 200m; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; keepalive_timeout 3; types_hash_max_size 2048; gzip on; gzip_disable msie6; gzip_static on; gzip_types text/plain text/css application/x-javascript text/xml application/xml+rss text/javascript; gzip_vary on; fastcgi_read_timeout 2m; include global/caching.conf; include /etc/nginx/enabled-sites/*; upstream php { server 127.0.0.1:9000; } #fastcgi caching header add_header mcapp-fastcgi-cache $upstream_cache_status; }
网站configuration:
server { server_name www.examplesite.com; rewrite ^ $scheme://examplesite.com$request_uri permanent; } server { #pagespeed directives pagespeed On; pagespeed FileCachePath /var/cache/nginx-pagespeed; location ~ "\.pagespeed\.([az]\.)?[az]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/ngx_pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } #pagespeed directives end server_name examplesite.com; root /path/to/examplesite; # wordpress config include global/restrictions.conf; include global/wordpress.conf; }
编辑只是为了进一步阐述,似乎不是压缩的具体资产是JavaScript的资产。 举个例子:
Enable compression for the following resources to reduce their transfer size by 355.5KiB (69% reduction). Compressing http://examplesite.com/wp-includes/js/jquery/jquery.js,qver=1.10.2.pagespeed.jm.iCH2ukpEYb.js could save 58.8KiB (64% reduction). Compressing http://examplesite.com/wp-content/themes/Avada/framework/plugins/revslider/rs-plugin/js/jquery.themepunch.revolution.min.js?ver=3.6.1 could save 43.9KiB (80% reduction).
在更多的头发拉,咬牙,打孔(和谷歌search)之后,我在NGINX支持论坛上发现了一个缺陷请求,将javascript(.js)mimetypes从application / x-javascript到应用程序/ JavaScript。 请参阅http://trac.nginx.org/nginx/ticket/306
正如你在我的问题中的nginx.conf所看到的,我有:
gzip_types text/plain text/css application/x-javascript text/xml application/xml+rss text/javascript;
这实际上导致我的javascript文件被gzip_types忽略,因为没有应用程序/ x-javascript mimetypes了(除非你在mime-types.conf中定制了一个,或者你有一个非常老的NGINX版本) 。
我改变了这一行:
gzip_types text/plain text/css application/javascript text/xml application/xml+rss;
NGINX重新加载后,我的JavaScript文件压缩得很好! 所以,事实certificate,这与PageSpeed模块无关,而是我的configuration有一个问题,不能识别正确的MIMEtypes进行压缩。
从版本1.9.32.1-beta开始,ngx_pagespeed将在nginx中不存在明确的gzipconfiguration(并且编译gzip压缩模块)时启用和configurationgzip本身。
请参阅https://developers.google.com/speed/pagespeed/module/release_notes#release_1.9.32.1-beta
根据RFC 4329 ,你的networking服务器应该使用application/javascript而不是application/x-javascript 。
首先,你应该检查你的/etc/nginx/nginx.conf文件是否包含(至less) gzip_types旁边的application/javascript :
例如
gzip_types text/plain text/css application/javascript text/xml application/xml+rss;
然后,打开你的MIMEtypes文件/etc/nginx/mime.types ,并确保如果你看到这个:
application/x-javascript js;
至
application/javascript js;
最后,重新加载你的configuration:
service nginx reload
而已!