Nginx Pagespeed does not Combine or Minify JS or CSS

authenticationmod-pagespeednginx

I am using Mod_Pagespeed Filters for Nginx and the combine and minify javascipt or css is not working. I checked everything and could not find the reason:

1) There are no errors in the error log

2) Pagespeed works cuz its able to collapse_whitespace, remove_comments etc..

3) The cache folder permission seems to be fine with chown to www-data

4) Tried adding Domain and LoadFromFile to see if that makes a difference but no:

pagespeed Domain http://example.com;
pagespeed LoadFromFile "http://example.com/assets/"
                        "/var/www/example/public_html/assets";

5) I have authentication lock set in my nginx like this (see below). When I disable auth-lock, the minify seems to work sometimes. I didint get a steady results to come to that conclusion:

location / {
                        auth_basic "Login";
                        auth_basic_user_file /etc/nginx/login_pass;
}

So my question is, if nginx authentication is enabled, would that fail pagespeed from minifying and combining JS or CSS? Or could it be failing for me due to something else? I dont know where to look next and I am so lost…

This is my Pagespeed filters:

# PageSpeed
# Enable ngx_pagespeed
pagespeed on;
pagespeed FileCachePath /usr/share/nginx/pagespeed;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
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_global_statistics { allow 127.0.0.1; deny all; }
location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }



pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters remove_comments;

# Defer and minify Javascript
# pagespeed EnableFilters defer_javascript;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters canonicalize_javascript_libraries;

# Inline and minimize css
pagespeed EnableFilters rewrite_css;
pagespeed EnableFilters fallback_rewrite_css_urls;
pagespeed EnableFilters combine_css;

UPDATE: It appears the authentication lock is not the one stopping pagespeed from combining and minimizing. I am thinking if any of the included JS is causing Pagespeed to not minify all the files if thats possible. There are about 6 CSS and 6 JS files to minify and combine. Still not sure why and looking into it.

SOLUTION: Turns out Pagespeed was unable to locate my static assets and hence it wasnt able to minify and combine the css. Mapping the static assets worked. Have added my answer below.

Best Answer

Found it! As it turns out, pagespeed was unable to find my css and js files and hence they were not combined nor minified. Thats why only collapse_whitespace and remove_comments worked. I need to Map the location of my static assets and then pagespeed was able to pick it up. So I added this to my pagespeed config and after that pagespeed worked like it should and started minifying and combined css and js files with no problems:

pagespeed LoadFromFile "http://example.com/"
                        "/var/www/example/html/";

Reading this link also helped me although this link was for ssl and mine wasnt: https://www.erianna.com/how-to-setup-nginx-pagespeed-with-ssl

More info on this here: https://developers.google.com/speed/pagespeed/module/domains#ModPagespeedLoadFromFile