Nginx Proxy Google Analytics
by John Vincent
Posted on April 26, 2017
Leverage Browser Caching Warning on Nginx
Page Speed flags browser caching of Google Analytics. The following is a way to address this.
I have seen this documented somewhere else, this is my implementation.
Html
Replace:
https://www.google-analytics.com/analytics.js
with
https://johnvincent.io/analytics.js
Nginx configuration
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
.......
location = /analytics.js {
proxy_pass https://www.google-analytics.com;
expires 31536000s;
proxy_set_header Pragma "public";
proxy_set_header Cache-Control "max-age=31536000, public";
}
Restart Nginx
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart nginx