mod_pagespeed not working with CDN domain

516 views
Skip to first unread message

Dan Murray

unread,
May 16, 2017, 9:34:06 AM5/16/17
to mod-pagespeed-discuss
Hi,

We are attempting to get mod_pagespeed working on our staging server for image optimisation only. We have a WordPress site where we can turn on and off CDN functionality to have all images served from either the CDN or the web server. If the CDN functionality is enabled then when the HTML is generated, image URLs are edited so that the server domain is replaced with the CDN domain. (When the CDN receives a request for an image, if it does not have that image in its cache, it requests it from the web server and adds it to its cache.)

The staging server domain is staging.mydomain.com

The staging CDN domain is stagingcdn.mydomain.com

I have tried to include the staging CDN domain in the pagespeed conf as well as the staging server domain.

We are using ModPagespeedInPlaceResourceOptimization so we expect to see image paths replaced with the pagespeed optimised versions.

If we have the CDN disabled then we see the staging server domain in the image URLs, and the paths being modified as expected by mod_pagespeed, for example:

<img src="https://staging.mydomain.com/wp-content/uploads/2016/07/xsignpost-image-28.jpg.pagespeed.ic.ov47tgitVG.webp" alt="" class="signpost__background" data-pagespeed-url-hash="2795913873" onload="pagespeed.CriticalImages.checkImageForCriticality(this);">

However if we have the CDN enabled then we see the CDN domain in the image URLs but we *do not* see the image paths being modified. For example we see:

<img src="https://stagingcdn.mydomain.com/wp-content/uploads/2016/07/signpost-image-28.jpg" alt="" class="signpost__background" data-pagespeed-url-hash="2034401332" onload="pagespeed.CriticalImages.checkImageForCriticality(this);">

Any help would be much appreciated!

All uncommented pagespeed.conf entries are listed below:

# Attempt to load mod_version if it wasn't loaded or compiled in (eg on Debian)
<IfModule !mod_version.c>
  LoadModule version_module /usr/lib64/httpd/modules/mod_version.so
</IfModule>

<IfVersion < 2.4>
  LoadModule pagespeed_module /usr/lib64/httpd/modules/mod_pagespeed.so
</IfVersion>
<IfVersion >= 2.4.2>
  # As default pagespeed.conf configuration uses old-style ACLs using
  # 'allow' and 'deny', we load mod_access_compat in Apache 2.4. If that's a
  # problem, the blocks using that can be converted to the newer 'Require'
  # syntax.
  <IfModule !access_compat_module>
    LoadModule access_compat_module /usr/lib64/httpd/modules/mod_access_compat.so
  </IfModule>

  LoadModule pagespeed_module /usr/lib64/httpd/modules/mod_pagespeed_ap24.so
</IfVersion>

# Only attempt to load mod_deflate if it hasn't been loaded already.
<IfModule !mod_deflate.c>
 LoadModule deflate_module /usr/lib64/httpd/modules/mod_deflate.so
</IfModule>
<IfModule pagespeed_module>
    # Turn on mod_pagespeed. To completely disable mod_pagespeed, you
    # can set this to "off".
    ModPagespeed on

    # We want VHosts to inherit global configuration.
    # If this is not included, they'll be independent (except for inherently
    # global options), at least for backwards compatibility.
    ModPagespeedInheritVHostConfig on

    # Direct Apache to send all HTML output to the mod_pagespeed
    # output handler.
    AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

    # If you want mod_pagespeed process XHTML as well, please uncomment this
    # line.
    # AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/xhtml+xml

    # The ModPagespeedFileCachePath directory must exist and be writable
    # by the apache user (as specified by the User directive).
    ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"

    # LogDir is needed to store various logs, including the statistics log
    # required for the console.
    ModPagespeedLogDir "/var/log/pagespeed"

    # The locations of SSL Certificates is distribution-dependent.
    ModPagespeedSslCertDirectory <snip>
    ModPagespeedSslCertFile <snip>

    # Override the mod_pagespeed 'rewrite level'. The default level
    # "CoreFilters" uses a set of rewrite filters that are generally
    # safe for most web pages. Most sites should not need to change
    # this value and can instead fine-tune the configuration using the
    # ModPagespeedDisableFilters and ModPagespeedEnableFilters
    # directives, below. Valid values for ModPagespeedRewriteLevel are
    # PassThrough, CoreFilters and TestingCoreFilters.
    
    ModPagespeedRewriteLevel PassThrough

    # Explicitly enables specific filters. This is useful in
    # conjuction with ModPagespeedRewriteLevel. For instance, filters
    # not included in the CoreFilters may be enabled using this
    # directive. This directive contains a comma-separated list of
    # filter names, and can be repeated.
   
    ModPagespeedInPlaceResourceOptimization off

    ModPagespeedEnableFilters rewrite_images
    
    # ModPagespeedDomain
    # authorizes rewriting of JS, CSS, and Image files found in this
    # domain. By default only resources with the same origin as the
    # HTML file are rewritten. For example:

    ModPagespeedDomain staging.mydomain.com
    ModPagespeedDomain stagingcdn.mydomain.com    
    ModPagespeedDomain https://staging.mydomain.com
    ModPagespeedDomain https://stagingcdn.mydomain.com
    ModPagespeedDomain *.mydomain.com
    
    # Other defaults (cache sizes and thresholds):    
    ModPagespeedFileCacheSizeKb          102400
    ModPagespeedFileCacheCleanIntervalMs 3600000
    ModPagespeedLRUCacheKbPerProcess     1024
    ModPagespeedLRUCacheByteLimit        16384
    ModPagespeedCssFlattenMaxBytes       102400
    ModPagespeedCssInlineMaxBytes        2048
    ModPagespeedCssImageInlineMaxBytes   0
    ModPagespeedImageInlineMaxBytes      3072
    ModPagespeedJsInlineMaxBytes         2048
    ModPagespeedCssOutlineMinBytes       3000
    ModPagespeedJsOutlineMinBytes        3000
    ModPagespeedMaxCombinedCssBytes      -1
    ModPagespeedMaxCombinedJsBytes       92160

    # Limit the number of inodes in the file cache. Set to 0 for no limit.
    # The default value if this paramater is not specified is 0 (no limit).
    ModPagespeedFileCacheInodeLimit        500000

    # Bound the number of images that can be rewritten at any one time; this
    # avoids overloading the CPU.  Set this to 0 to remove the bound.
    #
    
    ModPagespeedImageMaxRewritesAtOnce      0

    # These handlers are central entry-points into the admin pages.
    # By default, pagespeed_admin and pagespeed_global_admin present
    # the same data, and differ only when
    # ModPagespeedUsePerVHostStatistics is enabled.  In that case,
    # /pagespeed_global_admin sees aggregated data across all vhosts,
    # and the /pagespeed_admin sees data only for a particular vhost.
    #
    # You may insert other "Allow from" lines to add hosts you want to
    # allow to look at generated statistics.  Another possibility is
    # to comment out the "Order" and "Allow" options from the config
    # file, to allow any client that can reach your server to access
    # and change server state, such as statistics, caches, and
    # messages.  This might be appropriate in an experimental setup.
    <Location /pagespeed_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        Allow from <snip>
        SetHandler pagespeed_admin
    </Location>
    <Location /pagespeed_global_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        Allow from <snip>
        SetHandler pagespeed_global_admin
    </Location>

    # Enable logging of mod_pagespeed statistics, needed for the console.
    ModPagespeedStatisticsLogging on

    # Page /mod_pagespeed_message lets you view the latest messages from
    # mod_pagespeed, regardless of log-level in your httpd.conf
    # ModPagespeedMessageBufferSize is the maximum number of bytes you would
    # like to dump to your /mod_pagespeed_message page at one time,
    # its default value is 100k bytes.
    # Set it to 0 if you want to disable this feature.
    ModPagespeedMessageBufferSize 100000
</IfModule>


Thanks,

Dan.

Joshua Marantz

unread,
May 16, 2017, 10:43:30 AM5/16/17
to mod-pagespeed-discuss
Thanks for the detailed report & conf files.  Actually you have in-place resource optimization off, not on:
    ModPagespeedInPlaceResourceOptimization off
But you are right: this configuration should rewrite URLs that have been moved to the CDN.  My current suspicion (I'd need to see logs to be sure) is that HTTPS fetching is not working for you to your CDN.  To optimize an image that's already on the CDN, you to authorize the domain (which you did) and then mod_pagespeed needs to be able to successfuly fetch it from the CDN back onto your local host.

Can you check your apache logs to see if mod_pagespeed printed errors about fetching the files in question?  You might need to tweak your ssl-cert dir settings.


An alternative mechanism you could use that doesn't require fetching from the CDN is to do the CDN mapping in mod_pagespeed instead of WP.  You would use ModPagespeedMapRewriteDomain to add the CDN path to the URLs, but mod_pagespeed would be able to fetch them locally, or use LoadFromFile to bypass fetching.  But this shouldn't be required: MPS should be able to execute the configuration you have if it is fetching properly.



--
You received this message because you are subscribed to the Google Groups "mod-pagespeed-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mod-pagespeed-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mod-pagespeed-discuss/58db0b98-07bb-4e6d-9902-e959c4edb6b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohammad Owes

unread,
Feb 24, 2019, 11:35:54 AM2/24/19
to mod-pagespeed-discuss
Hi Dan,

Did you find any solution?  I am also having exact same problem where optimization is not being performed when using a CDN.

Thanks
Reply all
Reply to author
Forward
0 new messages