首先先按照一般步驟安裝 Nignx
並輸入 nginx -V 取得相關編譯參數。
apt install libgd-dev libgeoip-dev libxml2-dev libxslt-dev libssl-dev lua5.1-0-dev
下載以下所需的 module https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz https://github.com/openresty/set-misc-nginx-module/archive/v0.32.tar.gz https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version latest
--add-module=/root/incubator-pagespeed-ngx-latest-stable --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --sbin-path=/usr/sbin/nginx --add-module=/home/ngx_devel_kit-0.3.1rc1 --add-module=/home/set-misc-nginx-module-0.32 --add-module=/home/lua-nginx-module-0.10.13
修改 /etc/nginx/sites-enabled/default,並加入以下代碼
pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } pagespeed on; pagespeed RewriteLevel PassThrough; # 啟用壓縮空白過濾器 pagespeed EnableFilters collapse_whitespace; # 啟用JavaScript庫卸載 pagespeed EnableFilters canonicalize_javascript_libraries; # 把多個CSS文件合併成一個CSS文件 pagespeed EnableFilters combine_css; # 把多個JavaScript文件合併成一個JavaScript文件 pagespeed EnableFilters combine_javascript; # 刪除帶默認屬性的標籤 pagespeed EnableFilters elide_attributes; # 改善資源的可緩存性 pagespeed EnableFilters extend_cache; # 更換被導入文件的@import,精簡CSS文件 pagespeed EnableFilters flatten_css_imports; pagespeed CssFlattenMaxBytes 5120; # 延時加載客戶端看不見的圖片 pagespeed EnableFilters lazyload_images; # 啟用JavaScript縮小機制 pagespeed EnableFilters rewrite_javascript; # 啟用圖片優化機制 pagespeed EnableFilters rewrite_images; # 預解析DNS查詢 pagespeed EnableFilters insert_dns_prefetch; # 重寫CSS,首先加載渲染頁面的CSS規則 pagespeed EnableFilters prioritize_critical_css; location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { }
複製新版 Nginx
cp /usr/share/nginx/sbin/nginx /usr/sbin/nginx
重啟 Nginx
/etc/init.d/nginx restart
確認是否生效
curl -I http://localhost
若看到
X-Page-Speed: XXXXXX則表示成功