[null,null,["最后更新时间 (UTC):2024-04-17。"],[[["\u003cp\u003eThis guide details how to set up a private preview server for server-side tagging in Google Tag Manager using Cloud Run.\u003c/p\u003e\n"],["\u003cp\u003eA private preview server enhances security by routing traffic through a load balancer with host-based routing and an additional subdomain.\u003c/p\u003e\n"],["\u003cp\u003eThe setup involves updating environment variables, configuring the load balancer, and adjusting ingress control for the preview server.\u003c/p\u003e\n"],["\u003cp\u003eThis approach allows developers to preview tagging changes in a secure environment before applying them to the production tagging server.\u003c/p\u003e\n"],["\u003cp\u003ePath-based routing is not recommended as it may prevent events from reaching the correct destination; host-based routing provides better separation.\u003c/p\u003e\n"]]],["This guide details how to configure a private preview server for server-side tagging in Cloud Run. It focuses on routing traffic through a load balancer to restrict public access to the preview server. Key actions include updating the tagging server's `PREVIEW_SERVER_URL` environment variable to a new subdomain (e.g., `preview.sgtm.example.com`), configuring the load balancer for host-based routing, creating a new backend service, updating the ingress control for the preview server to internal and test the configuration.\n"],null,["# Set up a private preview server\n\n\u003e This guide is for developers using [Cloud Run](https://console.cloud.google.com/run) for server-side tagging who want\n\u003e to set up a private preview server.\n\nServer-side tagging lets you [preview](/tag-platform/tag-manager/server-side/debug) tagging changes in preview and debug mode\nbefore applying them. This is a critical part of the development workflow to\nensure your tags behave as intended. To enable this capability requires two\nCloud Run [deployments](/tag-platform/tag-manager/server-side/cloud-run-setup-guide.): one for a tagging server and one for a preview server.\nThis guide provides more information on how the two servers communicate and how\nyou can configure them in an enterprise environment.\n\nOverview\n--------\n\nWhen you deploy server-side tagging to Cloud Run you can deploy it with or\nwithout a load balancer. For multi-region deployments, you need a load balancer\nto direct traffic to the nearest instance. \n\n### Without load balancer\n\n\n*Figure 1: Deployment without a load balancer.*\n\n### With load balancer\n\n\n*Figure 2: Deployment with a load balancer.*\n\nIn both deployments, the entry point for both the preview server and production\ntagging environment is the same URL (for example, `sgtm.example.com`). The\ntagging server is deployed with an environment variable called\n`PREVIEW_SERVER_URL` which represents the URL of the preview server.\n\nWhen you visit `tagmanager.google.com` and open preview mode, Tag Manager\nnavigates to\n`sgtm.example.com?id=[gtm_server_id]>m_auth=[auth_id]>m_preview=[env_id]`\nand sets a cookie. When you then use the same browser to explore the target\nwebsite, requests sent to `sgtm.example.com` also forward the cookie. Because\nof the cookie, the tagging server knows these are your events, so it only\nforwards your events on to the preview server for you to view and troubleshoot.\n\nThe tagging server forwards these events by making HTTP requests to the URL set\nin the `PREVIEW_SERVER_URL` environment variable.\n\n### About public-facing preview servers\n\nThis default behavior requires your preview server to be public. However, this\nis problematic for some organizations as they have restrictions about what\ninfrastructure can be public facing and usually enforce additional security\nmeasures through a load balancer. A load balancer gives you:\n\n- More control over features such as SSL policies to restrict cipher suites.\n- Native integration with Cloud Armor for web-application firewall (WAF) or distributed denial-of-service (DDoS).\n- Advanced traffic-management capabilities.\n\nRoute your preview server through a load balancer\n-------------------------------------------------\n\nTo create a private preview server, use host-based routing through the load\nbalancer with an additional subdomain. The rest of this guide explains how to\nroute your preview server through a load balancer.\n\n### Prerequisites\n\n- Tagging server is [deployed](/tag-platform/tag-manager/server-side/cloud-run-setup-guide.) on Cloud Run behind a load balancer.\n- Access to the Google Cloud project.\n- Access to a new subdomain for the preview server, for exaple, `preview.sgtm.example.com`.\n\n### Step 1: Update the environment variable with a new subdomain\n\nTo use the new preview server domain, update the\nserver-side tagging server environment variable `PREVIEW_SERVER_URL`:\n\n1. Open [Cloud Run](https://console.cloud.google.com/run).\n2. Select the tagging server.\n3. Click **Edit** and **Deploy new revision**.\n4. Under the **Variables and secrets** tab, in the **Containers** section, change the `PREVIEW_SERVER_URL` to the new domain. For example: `preview.sgtm.example.com`.\n\n### Step 2: Configure the existing load balancer to use host-based routing\n\nTo configure the existing load balancer, use host-based routing and send\ntraffic to the correct server:\n\n1. In Cloud Run, open the [Load balancing](https://console.cloud.google.com/net-services/loadbalancing/list/loadBalancers) page and click the name of your load balancer.\n2. Click the **Edit** button.\n3. Click the **Backend configuration** page and open the drop down for **Backend services and backend buckets**.\n4. Click **Create a backend service** and complete the following:\n 1. Give it an appropriate name (for example, `preview-backend-service`).\n 2. Select the backend type as **Serverless network endpoint group**.\n5. In the **Backend** section, create a new serverless network endpoint group and complete the following:\n 1. Give it a name and select the region where the preview server is hosted.\n 2. Select **Cloud Run** as the serverless network endpoint group type, and the preview server as the service.\n 3. Click **Create**.\n6. Change other settings as needed, or keep the defaults, and click **Create**.\n\n### Step 3: Add a new rule and update the ingress control\n\nTo add a rule for the new host URL and update the ingress control:\n\n1. Open the **Host and path rules** page.\n2. Add a new rule where:\n - The host is the new URL: `preview.sgtm.example.com`\n - The path is: `/*`\n - The backend is: `preview-backend-service`\n3. Click the **Update** button to redeploy the load balancer.\n4. Back in [Cloud Run](https://console.cloud.google.com/run) open the preview server, and select the **Networking** tab.\n5. Change the **Ingress control** to **Internal** and check **Allow traffic from external application load balancers** so that it isn't publicly accessible.\n\n**Result** : Your server architecture should now look like *Figure 3*, where traffic requests are all\ndirected to the tagging server, and the tagging server is able to forward\nrequests to the preview server.\n\n*Figure 3: Using host-based routing.*\n| **Note:** You can't use path-based routing, for example, mapping `/gtm/debug` to `preview-backend-service`. Using this method you can view the debug window but can't see the events comes through. This is because the tagging server forwards events to other paths, and these can't get to the correct end destination. The host provides a clear separation between the two destinations.\n\nCheck your configuration\n------------------------\n\nTo check your configuration:\n\n1. [Open Google Tag Manager](https://tagmanager.google.com/#/home).\n2. Open your server container.\n3. Click **Admin \\\u003e Container settings** and set the server container URL to the tagging server URL.\n4. Close the window and preview the workspace. The debug window opens. As you navigate your website, your events should appear in the debug window.\n\nRelated links\n-------------\n\n- [Set up server-side tagging with Cloud Run](/tag-platform/tag-manager/server-side/cloud-run-setup-guide?provisioning=ui)\n- [Enable region specific behavior in server-side tagging](/tag-platform/tag-manager/server-side/enable-region-specific-settings#CloudRun)"]]