apache / incubator-pagespeed-ngx

Automatic PageSpeed optimization module for Nginx
http://ngxpagespeed.com/
Apache License 2.0
4.37k stars 365 forks source link

pagespeed not working #1327

Open brimmen opened 7 years ago

brimmen commented 7 years ago

Hi, I cant seem to get pagespeed module working. I have installed and setup the nginx.conf and domain.conf, but whichever settings I use from the official google pagespeed does not work referring to this previous issue https://github.com/pagespeed/ngx_pagespeed/issues/1129 my curl -I -p http://localhost HTTP/1.1 200 OK Server: nginx/1.11.6 Content-Type: text/html Connection: keep-alive Vary: Accept-Encoding Date: Thu, 01 Dec 2016 01:25:46 GMT X-Page-Speed: 1.11.33.4-0 Cache-Control: max-age=0, no-cache

I have a multiple VPS with lets encrypt https /usr/local/nginx/conf/nginx.conf: pagespeed on; pagespeed RespectVary on; pagespeed LoadFromFile "http://example.com/" "/var/www/example.com/"; pagespeed LoadFromFileRuleMatch disallow .*; pagespeed LoadFromFileRuleMatch allow .css$; pagespeed LoadFromFileRuleMatch allow .jpe?g$; pagespeed LoadFromFileRuleMatch allow .png$; pagespeed LoadFromFileRuleMatch allow .gif$; pagespeed LoadFromFileRuleMatch allow .js$;

This allows pagespeed to generate files on a non https domain, and for our https domain to serve them

    pagespeed MapOriginDomain "http://localhost" "http://www.example.com";
    pagespeed RewriteLevel PassThrough;
    pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
    }
    location ~ "^/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; }
    location /pagespeed_console { allow 127.0.0.1; deny all; }
    pagespeed EnableFilters rewrite_javascript,inline_google_font_css,collapse_whitespace,extend_cache,rewrite_css,sprite_images,defer_jav$
    pagespeed SslCertDirectory /etc/ssl/certs;
    pagespeed FetchHttps enable,allow_self_signed;
    pagespeed UseExperimentalJsMinifier on;
    }

}

Lofesa commented 7 years ago

Hi @brimmen PageSpeed seem to be working as you can see the X-Page-Speed: 1.11.33.4-0 and Cache-Control: max-age=0, no-cache headers. You had enabled only these filters : rewrite_javascript,inline_google_font_css,collapse_whitespace,extend_cache,rewrite_css,sprite_images,defer_jav None of these are combining css nor js nor image rewrite. When you use "pagespeed RewriteLevel PassThrough;" only filters explicitly enabled are working. Try "pagespeed RewriteLevel CoreFilters" or enable more filters like combine_css,combine_javascript,rewrite_images........ You can, too, run http://localhost?PageSpeedFilters=+debug and see the code of the page in any browser. Debug filter inserts comments about what pagespeed is doing And remember, pagespeed does not work at first hit, need some previous hits to see it working.

Hope this help

brimmen commented 7 years ago

Hi thanks for the reply. I have decided to rebuild a new server and start again. I tried the pagespeed RewriteLevel CoreFilters option but didn't make a difference. I have now added in my /usr/local/nginx/conf/nginx.conf and in my server block domain.com.conf an exhaustive list of filters for safe measure: pagespeed on; pagespeed RewriteLevel PassThrough; pagespeed FileCachePath "/var/cache/ngx_pagespeed/"; location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { add_header "" ""; } location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } pagespeed EnableFilters defer_javascript,sprite_images,recompress_png,convert_png_to_jpeg,convert_jpeg_to_webp,move_css_above_scripts,move_css_to_head,combine_css,combine_javascript,rewrite_images,prioritize_critical_css,inline_preview_images,resize_mobile_images,remove_comments,collapse_whitespace,lazyload_images,insert_dns_prefetch; } Whichever way I try and change the settings I get multiple instances of blocking CSS and JS resources in Pagespeed insights itself with an even worse pagespeed score, eg: https://domain.com/wp-content/plugins/cookie-notice/css/A.front.css,qver=ae30fb8b4561221aa99920709a1e19b2.pagespeed.cf.NTQaJGDiA-.css https://domain.com/wp-content/plugins/geo-my-wp/assets/css/A.style.css,qver=2.6.6.3.pagespeed.cf.y6gtaQhkBA.css

Am I missing something here in terms of the correct configuration. I also get "too many redirects" with http://localhost?PageSpeedFilters=+debug

I have lets encrypt running with cloudflare wordpress install on debian jessie.

Lofesa commented 7 years ago

Hi @brimmen To the "too many redirects" try to use http://localhost?PageSpeedFilters=%20debug All the resources with .pagespeed are rewriten by the module so NPS is working. Let´s encrypt is for ssl...are you using http/2? if yes, better don´t inline, combine or sprite any resources. Try to move all js files to the footer, with a plugin or if you have a child-theme put this hook in their functions.php

`function remove_head_scripts() { remove_action('wp_head', 'wp_print_scripts'); remove_action('wp_head', 'wp_print_head_scripts', 9); remove_action('wp_head', 'wp_enqueue_scripts', 1);

add_action('wp_footer', 'wp_print_scripts', 5); add_action('wp_footer', 'wp_enqueue_scripts', 5); add_action('wp_footer', 'wp_print_head_scripts', 5); } add_action( 'wp_enqueue_scripts', 'remove_head_scripts' );`

If you are using Cloudflare CDN this may had some conflict, try to put Cloudflare off line or in debug mode then do the PageSpeed Insights test, don´t forget to try it with NPS off too, to see if NPS make any difference. NPS has a cache for optimized resources and populate these cache takes some time. These time can conflict with Cloudflare cache storing non optimized or not fully optimized resources.

Hope this help

brimmen commented 7 years ago

Hi thanks for the swift reply. Ok so I am also using http/2. I had removed the inline, combine & sprite filters. I paused cloud flare with DNS pointing to VPS server. Scripts to footer are already enabled. I had this site previously working on another LEMP stack VPS, sans the Pagespeed & was getting a server response time of 2.1s . At present with my current config I get 3.8s with the many numerous Pagespeed blocking resources -seemingly contributing to the site being more cumbersome. I do have another VPS with two smaller sites running together https enabled with Pagespeed running, and getting about 0.39s speed, but also with Pagespeed blocking resources. As the main offending site in question is a busier site with a lot of essential plugins, I blocked the plugins to see the impact it had. And it did help, but still the blocking Pagespeed resources. Do you have a recommended template for the flavour of Pagespeed fit for purpose for my site, because I think I have entertained many options and come to an impasse; with the option to now do away with Pagespeed altogether.

Many thanks in advance

Lofesa commented 7 years ago

http-bloc.txt pagespeed.txt pagespeed_filters.txt

Hi @brimmen Well... I´m not an expert on NPS, only another user with many try and fail... I can send you the config files I use.... but there are no warranty to work fine in your sites.

I use a memcached server as the only cache store, you must configure the cache store to fit your needs. file http-bloc.txt are options that are included directly in the http block of the nginx conf file. file pagespeed.txt must be renamed pagespeed.conf and included in http block of nginx conf file. I don´t know why the parameters in http-block.txt can´t be included in the file pagespeed.conf.... file pagespeed_filters.txt must be renamed pagespeed_filters.conf and included in each virtual server config.

Hope this help