How to use same origin through Nginx
Updated Jul 31, 2025
Google now recommends a different approach for setting up custom domains in server-side Google Tag Manager. Instead of assigning a subdomain (for example, sgtm.example.com), the advised method is to use a same-origin path (such as example.com/sgtm).
The key advantage of same-origin for sGTM is the ability to establish first-party cookies seamlessly. Since the tagging server runs on the same domain as your site, there’s no need for extra configuration to extend cookie lifetimes. The trade-off, however, is that setting up same-origin is more technically complex compared to the simpler subdomain method.
Setup guide
Add to your server config, where:
- /metrics/ - is a path you choose for your server container.
- https://gtm.mysimple.name - custom domain of your sGTM.
location = /metrics { return 302 /metrics/; }
location ~ ^/metrics(.*) {
resolver 8.8.8.8 valid=3600s;
proxy_pass https://gtm.mysimple.name$1$is_args$args;
proxy_set_header Host gtm.mysimple.name;
proxy_set_header X-From-Cdn "cf-stape";
proxy_set_header CF-Connecting-Ip $remote_addr; #If you do not use Cloudflare for your domain
#proxy_set_header CF-Connecting-Ip $http_cf_connecting_ip; #If you use Cloudflare for your domain
}
Custom Loader
Once the same origin setup is done, we also recommend updating Custom Loader for ad blocker protection and implementing it on your site. Don’t forget to specify your Same Origin path in it.

For more information, see our guide on Custom Loader.
Comments