Follow this Blog!

Jan 27, 2016

f Comment

Google PageSpeed Module in Nginx: "pagespeed ModifyCachingHeaders off" Not Always Working! Here's how to fix it!

AmazonOne day I realized the Nginx directive "pagespeed ModifyCachingHeaders off" is not working for my static CSS and JS files, but is working with other HTTP requests. Why? How do you fix it so that Google PageSpeed will never modify my HTTP caching header fields in the HTTP responses sent by my web server?

What's happening?

Let's assume in my Nginx configuration I have the following line:
pagespeed ModifyCachingHeaders off
Let's assume the original caching header field for http://www.chtoen.com/css/chtoen.css is the following:
Cache-Control:max-age=2592000
When I turn off pagespeed and go to http://www.chtoen.com/css/chtoen.css I see the following HTTP header fields in the response:
Cache-Control:max-age=2592000
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/css
Date:Mon, 25 Jan 2016 03:31:19 GMT
Expires:Wed, 24 Feb 2016 03:31:19 GMT
Last-Modified:Sun, 24 Jan 2016 08:53:26 GMT
Server:nginx/1.4.6
Transfer-Encoding:chunked
Vary:Accept-Encoding
I turn on pagespeed and go to http://www.chtoen.com/css/chtoen.css. Here are the HTTP header fields:
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/css
Date:Mon, 25 Jan 2016 03:43:14 GMT
Server:nginx/1.4.6
Transfer-Encoding:chunked
Vary:Accept-Encoding
In other words, "pagespeed ModifyCachingHeaders off" is not working for my static CSS file. Why not? Read on to find a solution.

Solution

The solution is easy: Disable certain URLs from processing by PageSpeed module.

I add the following to my Nginx configuration file:

pagespeed Disallow "*.js";
pagespeed Disallow "*.css";

Reload Nginx and here are the HTTP header fields for http://www.chtoen.com/css/chtoen.css:
Cache-Control:max-age=2592000
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/x-javascript
Date:Mon, 25 Jan 2016 03:44:35 GMT
Expires:Wed, 24 Feb 2016 03:44:35 GMT
Last-Modified:Sun, 24 Jan 2016 08:53:26 GMT
Server:nginx/1.4.6
Transfer-Encoding:chunked
Vary:Accept-Encoding
As you can see, PageSpeed module no longer modifies my caching header fields.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael