Google PageSpeed module Nginx configuration for Origin Pull CDNs

We know that "Origin Pull" Content Delivery Networks only require us to change the static file URLs (CSS, JS, images, etc.) so that these static files will be downloaded from a CDN instead of our web server.

Most of the Google PageSpeed filters change the static file URL which denotes that Google PageSpeed has already processed or optimized this static file. If we are to implement Origin Pull CDN, we need to make sure that the CDN will fetch (pull) the Google PageSpeed optimized static files from our server (origin). Of course, Google PageSpeed can handle this task with the right configuration. The configuration that will be presented here is for Nginx.

To install Nginx with PageSpeed module please follow the steps here. Only Nginx code snippet will be shown here, if you need the full Nginx configuration please check this article.

  1. Login to your CDN and create a Pull zone for your website's domain. Take note of the generated Zone URL.

    Note: If your website is running under SSL, specify it in your "Origin URL" field e.g. https://www.yourdomain.com and make sure the SSL option in your CDN is enabled (if you selected custom, upload your SSL certificate and private key files).

  2. It is highly recommended to use your own custom CDN URL or Zone URL (e.g. cdn.yourdomain.com) instead of the URL generated by your CDN.

    Login to your domain name provider and add the Zone URL alias (e.g. cdn.yourdomain.com) to the DNS Records as CNAME and map it to the Zone URL generated by your CDN.

  3. Edit the Nginx server block of your website's domain and add the PageSpeed settings snippet shown below (the configuration below assumed that your website is running under SSL):

          
    server {
      listen XXX.XXX.XXX.XXX:443 ssl http2;
      server_name www.yourdomain.com;
    
      ## PageSpeed settings
      pagespeed EnableFilters rewrite_domains;
      pagespeed Domain https://www.yourdomain.com;
      pagespeed MapRewriteDomain https://cdn.yourdomain.com https://www.yourdomain.com;
    
      ...
    }
          
        

    Note: Replace all the occurrence of XXX.XXX.XXX.XXX with your server's IPv4 address.

    For multiple Zone URL aliases, use the configuration below:

          
    server {
      listen XXX.XXX.XXX.XXX:443 ssl http2;
      server_name www.yourdomain.com;
        
      ## PageSpeed settings
      pagespeed EnableFilters rewrite_domains;
      pagespeed ShardDomain https://www.yourdomain.com https://cdn1.yourdomain.com,https://cdn2.yourdomain.com;
    
      ...
    }
          
        
  4. Restart Nginx:

          
    systemctl restart nginx
          
        

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.