4

If a server hosts the following domains:-

http://exampleA.com
http://exampleB.com
http://exampleC.com

How is it possible to enable mod_pagespeed for only domains http://exampleA.com and http://exampleC.com but not http://exampleB.com?

I have read up on the documentation for domains but not quite sure I can fathom out if this possible to achieve...

I have tried inserting the following into pagespeed.conf and restarting Apache but mod_pagespeed is still applying to all domains...

ModPagespeedDomain http://exampleA.com
ModPagespeedDomain http://exampleC.com

2 Answers 2

4

After a little digging and playing around with this, I've managed to figure it out and now have a working scenario for mod_pagespeed to only work on specific domains (vhosts) on the same server.

My configuration is based on a CentOs 6 build Apache 2 that runs Parallels Plesk Panel.

Create a separate pagespeed conf file and store it in a location that won't be a) automatically overwrote or b) automatically loaded (i.e in the existing conf or conf.d directories). In my instance, I created /etc/httpd/myconf and named the file vhosts-pagespeed.conf.

This configuration file will contain themod_pagespeed configuration you wish specific domains to use (you can create as many of these as you like). For example, mine is like:-

#https://developers.google.com/speed/pagespeed/module/configuration#virtual-hosts

<IfModule pagespeed_module>
ModPagespeed on
AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html
ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"
ModPagespeedFileCacheInodeLimit        500000
ModPagespeedAvoidRenamingIntrospectiveJavascript on
ModPagespeedLibrary 105527 ltVVzzYxo0 //ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js
ModPagespeedLibrary 92501 J8KF47pYOq //ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js
ModPagespeedLibrary 141547 GKjMUuF4PK //ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js
ModPagespeedLibrary 43 1o978_K0_L http://www.modpagespeed.com/rewrite_javascript.js

ModPagespeedEnableFilters extend_cache
ModPagespeedEnableFilters rewrite_javascript
ModPagespeedEnableFilters rewrite_css
ModPagespeedEnableFilters combine_javascript
ModPagespeedEnableFilters combine_css
ModPagespeedEnableFilters inline_javascript
ModPagespeedEnableFilters inline_css
ModPagespeedEnableFilters insert_img_dimensions
ModPagespeedEnableFilters move_css_to_head
ModPagespeedEnableFilters lazyload_images
ModPagespeedEnableFilters rewrite_images
ModPagespeedEnableFilters outline_css
ModPagespeedEnableFilters flatten_css_imports
ModPagespeedEnableFilters inline_import_to_link
ModPagespeedEnableFilters local_storage_cache
ModPagespeedEnableFilters collapse_whitespace
ModPagespeedEnableFilters elide_attributes
ModPagespeedEnableFilters remove_comments

ModPagespeedDisableFilters rewrite_javascript,combine_javascript

ModPagespeedModifyCachingHeaders off

ModPagespeedDomain *
<Location /mod_pagespeed_beacon>
      SetHandler mod_pagespeed_beacon
</Location>
<Location /mod_pagespeed_statistics>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_statistics
</Location>
<Location /mod_pagespeed_console>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_console
</Location>
<Location /mod_pagespeed_message>
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_message
</Location>
<Location /mod_pagespeed_referer_statistics>
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_referer_statistics
</Location>
</IfModule>

You'll then need to disable the global mod_pagespeed and only include the vhost specific pagespeed config on those domains which you wish to enable mod_pagespeed.

Navigate to /var/www/vhosts/domain.com/conf and create the file vhost.conf with the include (this is all this file needs to contain):-

Include /etc/httpd/myconf/vhosts-pagespeed.conf

Disable global /etc/httpd/conf.d/pagespeed.conf by modifying like so:-

ModPagespeed off

Then restart the server:-

service httpd restart

Now check that mod_pagespeed is enabled on the domain.com that you extended the vhost to include the custom pagespeed conf.

If that doesn't work, you may find you need to reconfigure the vhost for domain.com with something like:-

 /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=domain.com
7
  • 1
    Nicely done! This can likely also be applied to similar or near similar questions and be very helpful to users.
    – closetnoc
    Commented Jan 6, 2015 at 14:43
  • I hope so, I was sure this must have been possible but surprisingly found very little on the web discussing it. Thanks.
    – zigojacko
    Commented Jan 6, 2015 at 14:57
  • Sometimes I am surprised what is not covered on the net. Somethings I would think would be obvious cannot be found. Part of it sometimes is over optimization by Goolge- found answer on page 34 or something. ;-) Now it will be on page 1!
    – closetnoc
    Commented Jan 6, 2015 at 15:01
  • 1
    On a side note: mod_pagespeed can also be toggled from the htaccess file
    – Meki
    Commented Jan 6, 2015 at 16:36
  • 1
    @zigojacko Your assumption is correct! In our case We have disabled mod_pagespeed globally, and used the htaccess file to enable it selectively for some of our clients (We've got lots of legacy pages and in some cases mod_pagespeed breaks the page). Also the htaccess can be used to selectively enable/disable mod_pagespeed on a folder basis... for example: enable it on the visitor site, disable it on the admin interface.
    – Meki
    Commented Jan 7, 2015 at 9:51
3

If you can use .htaccess files you can simply set ModPagespeed off in the httpd.conf and then do this in the root .htaccess for any domain you want it enabled on:

<IfModule pagespeed_module>

    ModPagespeed on

</IfModule>

If you are using Apache 2.4+ you can even use the <If> directive for cases where the same .htaccess file is used for multiple domains, or for example if you have the .htaccess in a Git repository and you use the same one for the production and development sites but want ModPagespeed on only for the production site:

<IfModule pagespeed_module>

    <If "req('Host') == 'www.example.com'">
        ModPagespeed on
    </If>
    <Else>
        ModPagespeed off
    </Else>

</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.