How to use same origin through Nginx
Updated Jan 16, 2026
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
}If you are not using your own subdomain and are proxying requests to the standard Stape subdomain, you also need to add the X-Stape-Host header with the value of the host where the events are taking place (for instance, X-Stape-Host: www.example.com).
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.
Testing your Same Origin setup
You can check that everything is configured correctly by sending a single test request using a GA4 tag.
1. Create a GA4 tag in your web GTM container.
Add your Measurement ID (any ID works for testing) and set the server_container_url to the same-origin path you configured (we'll use https://stape.work/metrics as an example).

2. (Optional) To streamline testing, add your same-origin path (in this example, it's https://stape.work/metrics) to your server GTM settings. Click Admin → Container Settings → enter the path under Server container URLs.

3. Open Preview mode in both your web GTM container and your server-side GTM container. If everything is set up correctly, the server container should open in preview at the new path you created.

4. Trigger a page view on your site. In the server container preview, you should now see an incoming page_view request from your website’s real domain.

Comments