Applicable to:
- Plesk for Linux
Question
How to enable or disable the Google PageSpeed module in Nginx for a domain in Plesk?
Answer
Warning: Due to a CentOS Kernel Bug, do not enable PageSpeed in nginx when SELinux is in enforcing mode
-
Connect to a Plesk server via SSH
-
Enable the Google PageSpeed module for nginx:
# plesk sbin nginx_modules_ctl -e pagespeed
-
Create a directory where nginx cache will be stored and adjust permissions and ownership:
# mkdir /var/cache/nginx_example_com/
# chown nginx:nginx /var/cache/nginx_example_com/
# chmod 700 /var/cache/nginx_example_com/ -
Go to Domains > example.com > Apache and nginx settings
-
Add the following lines into the Additional nginx directives and click OK to apply the changes:
Note: Replace
FileCachePath
with path created in Step 3 where the Nginx will store the cacheCONFIG_TEXT: pagespeed on;
pagespeed FileCachePath "/var/cache/nginx_example_com/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { } -
To verify that the module is enabled, run the command on the server directly:
# curl -Is https://example.com | grep X-Page-Speed
X-Page-Speed: 1.13.35.2-0
-
Connect to the server via SSH
-
Find the domains with PageSpeed enabled:
# grep -l pagespeed /var/www/vhosts/system/*/conf/vhost_nginx.conf | cut -d '/' -f6
-
For every domain found in step 2 go to Domains > example.com > Apache and nginx settings
-
Remove the following lines from Additional nginx directives and click OK to apply the changes:
CONFIG_TEXT: pagespeed on;
pagespeed FileCachePath "/var/cache/nginx_example_com/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { } -
Over SSH disable the Google PageSpeed module for nginx:
# plesk sbin nginx_modules_ctl -d pagespeed
-
Optionally, remove the cached files for each domain:
# rm -rf /var/cache/nginx_example_com/
Comments
25 comments
Centos ...what issue might we expect`? Or might they be already fixed?
Hello @Robert
On Centos due to CentOS 7 kernel bug #15680 it is possible that there wouldn't be optimized static files on the website with the Pagespeed Insights extension installed in Plesk: https://support.plesk.com/hc/en-us/articles/360014944693-Pagespeed-does-not-optimize-static-files-and-throws-errors-in-Apache-logs
As a workaround, until the issue is fixed permanently by CentOS team, it's recommended to disable the SELinux on the server.
Excluding this bug, I did not find other OS related issues.
I enabled it but did not make any difference to website. Do I need to do some configuration for website performance? If yes How can I do it.
Hello @Sam Smith,
Thank you for your question.
To troubleshoot the website performance I recommend you to check the following articles:
I also enabled it and did not notice any difference when checking it against google PageSpeed Insights. I also followed Daria links and it did not help. Is there any PageSpeed configuration file that needs to be modified... maybe nginx conf? it seems as though something is missing as no difference before and after was noticed on PageSpeed Insight test.
I checked my server and installled the mod according to the directions and it is installed.
[root@serv08 ~]# plesk sbin nginx_modules_ctl --status
pagespeed on
help?
Enabled mod_pagespeed for nginx too, it's better enabled or leave disabled the "Smart static files processing"?
@Alex Morales I recommend submitting a ticket to support or post the issue on a forum if you need assistance.
@Remigio Ruberto you may try both and see where the score is better for your site. "Smart static files processing" means Nginx server static files and generally it supposes to do it better.
@Anton Maslov thanks but I am not interested in opening a support ticket.
--------
I found a solution that worked for me.
@Sam Smith, this was probably why it did not work for you either....
The instructions above worked flawlessly for installing Google PageSpeed module for nginx in Plesk . It installs it yes, HOWEVER it is DISABLED and missing some steps to get it to work.
The steps below will turn on the Google PageSpeed module - it worked for me, hope it works for everyone else too.
Step one:
create the directory /var/cache/nginx/ and change ownership to nginx.
# chown nginx:nginx /var/cache/nginx/
Step two:
In the "Additional nginx directives" section in the Plesk admin, you will need to add the following to turn on the Google PageSpeed module on a per site basis:
pagespeed on;
pagespeed FileCachePath "/var/cache/nginx/";
pagespeed RewriteLevel OptimizeForBandwidth;
pagespeed RewriteLevel CoreFilters;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
Step three:
Now check to see if Page Speed is running on that domain.
# curl -I -X GET https://yourdomain.com
HTTP/1.1 200 OK
Server: nginx
X-Page-Speed: 1.13.35.2-0 <-------------- this line should be here but your version may vary.
Hi Alex,
It makes sense what you are saying. I have update article, thank you very much for the input. The only thing here it is better to keep domain and global nginx cache separately, so you can create separate directory for domain and specify it in the additional nginx directives.
Hi Alex @Alex Morales
How can I create directory and change ownership to nginx>
Thanks
Sam
Hello Sam,
Just follow instructions provided in the article:
1. Login server via SSH;
2. Run the commands:
# mkdir /var/cache/nginx_example_com/
# chown nginx:nginx /var/cache/nginx_example_com/
# chmod 700 /var/cache/nginx_example_com/
I am getting the following Error:
plesk sbin nginx_modules_ctl --enable pagespeed
START nginx_modules_ctl --enable pagespeed
/opt/psa/admin/sbin/nginx-config execution failed:
nginx: [emerg] "load_module" directive is specified too late in /etc/nginx/modules.conf.d/pagespeed.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
/opt/psa/admin/sbin/nginx-config execution failed:
nginx: [emerg] "load_module" directive is specified too late in /etc/nginx/modules.conf.d/pagespeed.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
exit status 1
Hello @Taklos,
Sorry for the late response.
Generally, the error is self-explanatory. It means that "load_module" directive is defined in an incorrect place.
Could you show the content of /etc/nginx/modules.conf.d/pagespeed.conf?
Alternatively, to speed up the investigation, submit a request to our Technical Support: https://support.plesk.com/hc/en-us/articles/213608509-How-to-submit-a-request-to-Plesk-support-
@Taktlos
Hi,
Such issues may occur if load_module directive is defined after "event {...}" block in nginx configuration. By default, the main configuration file of nginx /etc/nginx/nginx.conf has such structure:
I recommend you to check the main configuration file /etc/nginx/nginx.conf and make sure "include /etc/nginx/modules.conf.d/*.conf;" line stays above "event {...}" block.
I want to say a little to the solution of Alex Morales. It is not correct to define both types of RewriteLevel together. If you want to use CoreFilters, then you have to define nothing, because these Core Filters are already set by default. Only if you want use RewriteLevel OptimizeForBandwidth you have to write this line in the config (nginx.conf), like Alex did, but never ever both types.
For those who don't know whether to serve their web with nginx + apache&pagespeed, or apache&pagespeed, or nginx&pagespeed.
For me, who mostly hosts prestashop websites, the best option is to use nginx&pagespeed.
I have tried all the options, and finally the best one is this one. Without hesitation.
I don't know why, but it's true. I'm using the same pagespeed configuration as with apache (translated to nginx syntax) and... the difference is remarkable.
I think that with nginx+apache, TTFB gains some milliseconds, and maybe with every query. And that makes it take quite a bit longer, because Prestashop performs many queries to build a view.
To give you an idea, a fully loaded web can take between 2-6 seconds with nginx+apache&pagespeed, but it goes down to 0.7-2 seconds when using only nginx&pagespeed.
If we add to that, that it's much easier to manage each web individually...
The option is clear (until someone shows me that I got confused in my tests with nginx+apache&pagespeed+prestashop*N)
Hello Nerque
Thank you for sharing your user experience.
Currently, your tests look legit.
Pleskians, in case you have something to discuss, feel free to participate.
Does anyone know why I'd be getting a 500 error for the ngx_pagespeed_beacon?
pagespeed on;
pagespeed FileCachePath "/var/cache/nginx_pek3_com";
pagespeed EnableFilters rewrite_images;
pagespeed RewriteLevel OptimizeForBandwidth;
Hello Daniel Slyman
Additional investigation is required to find the root cause.
Consider submitting a support request.
Ivan Postnikov
It was due to the Adguard Adblocker
Hello Daniel Slyman
Thank you for letting me know.
Plesk command not found. Is it not include in Plesk obsidian.
Nerque, would you mind posting the settings that you considered the best/nginx directives you are using? I have a customer running a few Wordpress websites that are suffering with bad performance (consuming too many server resources) and I would like to try your approach.
How to enable pagespeed for nginx server-wide?
Please sign in to leave a comment.