Configuration tutorial of ngx_pagespeed that is not enough
in Tutorial with 27 comments
Configuration tutorial of ngx_pagespeed that is not enough
in Tutorial with 27 comments

From the previous article, we learned how to install ngx_pagespeed on your nginx. This article will tell you how to configure and use ngx_pagespeed to speed up the front-end resources of your site. Some of these configurations need to be filtered and used according to the situation of your own site. For example, when your site's CSS is always updated, you should not configure compression, consolidation, cascading, and inlining of CSS.

Create cache folder

 mkdir /var/ngx_pagespeed/ chown www.www /var/ngx_pagespeed/

Configure conf file

Open your site's conf profile

Suppose my profile is in /usr/local/nginx/conf/vhost Contents of

 vim /usr/local/nginx/conf/vhost/www.linpx.com.conf

stay server {} Add the following configuration code in

 #On Enable, Off Off pagespeed on; #Reset http Vary header pagespeed RespectVary on; #Html characters to lowercase pagespeed LowercaseHtmlNames on; #Compress resources marked with Cache Control: no transform #pagespeed DisableRewriteOnNoTransform off; #Relative URL #pagespeed PreserveUrlRelativity on; pagespeed XHeaderValue "Powered By LiNPX"; #Turn on https #pagespeed FetchHttps enable; #Configure the server cache location and auto clear trigger conditions (space size, time limit) pagespeed FileCachePath "/var/ngx_pagespeed/"; pagespeed FileCacheSizeKb 2048000; pagespeed FileCacheCleanIntervalMs 43200000; pagespeed FileCacheInodeLimit 500000; #Filter Rules pagespeed RewriteLevel PassThrough; #Filter the/wp admin/directory of WordPress (optional configuration, refer to) pagespeed Disallow "*/wp-admin/*"; pagespeed Disallow "*/wp-login.php*"; #Filter the/admin/directory of the typecho (optional configuration, refer to) pagespeed Disallow "*/admin/*"; #Removing unnecessary url prefixes and enabling them may cause some automatic loading functions to fail #pagespeed EnableFilters trim_urls; #Remove html whitespace pagespeed EnableFilters collapse_whitespace; #Remove html comment pagespeed EnableFilters remove_comments; #DNS Preload pagespeed EnableFilters insert_dns_prefetch; #Compress CSS pagespeed EnableFilters rewrite_css; #Merge CSS pagespeed EnableFilters combine_css; #Rewrite CSS and optimize CSS rules for loading rendered pages pagespeed EnableFilters prioritize_critical_css; #Google font is directly written into html to reduce browser requests and DNS queries pagespeed EnableFilters inline_google_font_css; #Compress js pagespeed EnableFilters rewrite_javascript; #Merge js pagespeed EnableFilters combine_javascript; #Refine inline style properties #pagespeed EnableFilters rewrite_style_attributes; #Compress Picture #pagespeed EnableFilters rewrite_images; #Do not load pictures outside the display area pagespeed LazyloadImagesAfterOnload off; #Picture preload pagespeed EnableFilters inline_preview_images; #Mobile terminal picture adaptive reset pagespeed EnableFilters resize_mobile_images; #Picture delay loading pagespeed EnableFilters lazyload_images; #Sprite pictures are very useful when there are many icons #pagespeed EnableFilters sprite_images; #Expand cache to improve the cacheability of page resources pagespeed EnableFilters extend_cache; #Cannot delete location ~ "\.pagespeed\. ([a-z]\.)? [a-z]{2}\. [^.]{10}\. [^.]+" { add_header "" ""; } location ~ "^/ngx_pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } location /ngx_pagespeed_statistics { allow 127.0.0.1;  deny all; } location /ngx_pagespeed_message { allow 127.0.0.1;  deny all; }

Optimize configuration

stay pagespeed on; Add the Memcached configuration under (Memcached is required first)

 pagespeed MemcachedThreads 1; pagespeed MemcachedServers "localhost:11211";

Restart Nginx

Please remember to restart Nginx after configuration!

CentOS 6.x:

 service nginx restart

CentOS 7.x:

 systemctl restart nginx

to update

Modify the configuration file in more detail. This time it should be enough.

The final conclusion: because the work requires ngx_pagespeed to be used for other sites of the company, this tutorial was created. However, I delayed writing for a long time, and there may be errors and omissions. Welcome to add and improve.

Responses
  1. Envy of opening such a fast blog.

    Reply
  2. Zen Cat

    This module consumes more resources

    Reply
    1. @Zen Cat

      True:)

      Reply
  3. The annotation of this pagespeed is very detailed~like one

    Reply
  4. I am linked from the theme. ngx_pagespeed is very useful

    Reply
    1. @Papaya Garden

      In my personal opinion, pagespeed is particularly useful for websites that are bloated, multi graph, and multi js.

      Reply
  5. Nice sharing, thanks

    Reply
    1. @Kaige We Media

      Thank you for your return visit:)

      Reply
  6. ZeroClover

    Recently, the cPanel has been completed, and the cPnginx in it also has a pagespeed module for fun

    Reply
    1. @ZeroClover

      Thank you for your visit:)

      Reply