Nmmapper.com

Menu

Last update on .

Nginx webserver and reverse proxy.
Nginx webserver and reverse proxy.

Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The reverse proxy part plays a major role in delegating requests to other web-servers their by making it suitable for for dealing with ad-blockers.

Ad blocking or ad filtering is a software capability for removing or altering online advertising in a web browser or an application. This have become very popuplar this days that the huge percentage of internet users install adblockers in there browsers. Even browser markers also have option to enable adblockers, this is in opera and firefox also has it's option of not allowing trackers to keep prying on your browsing habbit.

Some adblockers are so aggressive that they even block analytic software the famous one being google analytics i spent days to weeks to months finding a way of tracking adblock users who are using adblockers and thus hiding from google analytics, there seems to be sites offering options to deal with that. But I wanted adblockers to be shown in my google analytic dashboard. Finally i found a solution of using NGINX and a little modification on the analytic script as we are going to see.

Popular list of adblockers

  • Ublock origin( very aggressive)
  • Ghostery
  • Adlock
  • Adblock Plu

Requirements for configuring nginx for google analytic

The most basic requirement that I notice is a subdomain of your site if your site is www.example.com then create a subdomain like subcode.example.com I recommend it be running an https, you can use let's encrypt


Cofiguring Nginx for Google Analytics Proxy

server {
     server_name subcode.example.com;

     location /.well-known/acme-challenge/ {
        allow all;
        root /var/www/letsencrypt;
        try_files $uri =404;
        break;
     }
    
     location / {
        proxy_pass http://www.google-analytics.com/;
     }

     location /u/d/{
        proxy_set_header Accept-Encoding "";

        sub_filter 'www.google-analytics.com'  'subcode.example.com';
        sub_filter_types *;
        sub_filter_once off;

        proxy_pass https://www.google-analytics.com/u/d;
        break;
     }


     location /gtm/js/{
        proxy_set_header Accept-Encoding "";

        sub_filter 'www.google-analytics.com'  'subcode.example.com';
        sub_filter_types *;
        sub_filter_once off;

        proxy_pass https://www.google-analytics.com/gtm/js;
        break;
     }
 
     location /plugins/ua/{
        proxy_set_header Accept-Encoding "";

        sub_filter 'www.google-analytics.com'  'subcode.example.com';
        sub_filter_types *;
        sub_filter_once off;

        proxy_pass https://www.google-analytics.com/plugins/ua;
        break;
     }

     location = /analytics.js {
        # you have to compile nginx with http://nginx.org/en/docs/http/ngx_http_sub_module.html (this is not default)
        # and http://nginx.org/en/docs/http/ngx_http_proxy_module.html (it's a default module)
        proxy_set_header Accept-Encoding "";
        sub_filter 'www.google-analytics.com'  'subcode.example.com';
        sub_filter 'r/collect' 'rrr/upstreamer';
        sub_filter_types *;
        sub_filter_once off;

        proxy_pass https://www.google-analytics.com/analytics.js;
        break;
    }
   
    location /rrr/upstreamer/{
        proxy_set_header Accept-Encoding "";

        sub_filter 'www.google-analytics.com'  'subcode.example.com';
        sub_filter_types *;
        sub_filter_once off;

        proxy_pass https://www.google-analytics.com/r/collect;
        break;
    }

    location /gtag/js/{
        proxy_set_header  Accept-Encoding "";
        sub_filter  "www.googletagmanager.com"  "subcode.example.com";
        sub_filter_types *;
        sub_filter_once off;
        
        proxy_pass https://www.googletagmanager.com/gtag/js?id=xxxxx-1;
        break;
    }
}

 

Quick explanation on the location in configuration snippet above

Notice the following location block

  • location = /analytics.js Later on, on your webpage subcode.example.com  will make a request to subcode.example.com/analytics.js and will be handled by the above code block in nginx configurations. also you will notice this snippet undert his code block sub_filter 'www.google-analytics.com'  'subcode.example.com'; Here we are instructing nginx to replace all the occurence of www.google-analytics.com with 'subcode.example.com'. This now explains why we have other code block in our nginx configurations, because after the replacement, all requests to communicate with google analytic service will be made from our server which will be pased to google analytics, like this proxy_pass https://www.google-analytics.com/plugins/ua;
     break;
  • location /rrr/upstreamer/: When the analytic script is successfuly installed on your html code it will make a request to 'www.google-analytics.com/r/collect' but all adblockers will block '/r/collect' request so to overcome this blocking behavior will again replace /r/collect/ with sub_filter 'r/collect' 'rrr/upstreamer'; in the location block explained above. such that the downloaded analytic script will have it replaced before reaching the browser. So in summary the request will be made like this subcode.example.com/rrr/upstreamer/ and we just pass it to the original server proxy_pass https://www.google-analytics.com/r/collect;
            break;

So you may need to look for any request url and add it to your nginx block such that the request is not 404 that means you browser around the analytic script to find out which url is being made to your server.
 

Replace the analytic script url to your domain url

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//your.domain.com/analytics.js','ga');
# after this run your regular ga code
ga('create', 'UA-XXXXXXX-Y', 'auto');
ga('set', 'userId', 'your logged user id');
ga('send', 'pageview');


You may need to configure nginx to pass all user request to google analytic things like user IP because google analytics bases from this IP to get the location of the request. If not IP is passed the request will be logged in google analytic but the location will be the location of the server.

You can actually apply this same technique to proxy other scripts you know are blocked. So if you are reading this article with adblocker on, We still tracking you.

 

From our friends

Similar entries