kind:compute#urlMapname:web-map-httpdefaultUrlRedirect:redirectResponseCode:MOVED_PERMANENTLY_DEFAULThttpsRedirect:Truetests: - description:Test with no query parametershost:example.compath:/test/expectedOutputUrl:https://example.com/test/expectedRedirectResponseCode:301 - description:Test with query parametershost:example.compath:/test/?parameter1=value1¶meter2=value2expectedOutputUrl:https://example.com/test/?parameter1=value1¶meter2=value2expectedRedirectResponseCode:301
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Set up an HTTP-to-HTTPS redirect for regional external Application Load Balancers\n\nThis page shows you how to set up an HTTP-to-HTTPS redirect for the\nregional external Application Load Balancer. This page is for a regional external Application Load Balancer only. If\nyou use a load balancer in a different mode, see one of the following\npages:\n\n- [Set up an HTTP-to-HTTPS redirect for\n global external Application Load Balancers](/load-balancing/docs/https/setting-up-global-http-https-redirect)\n\n- [Set up an HTTP-to-HTTPS redirect for a\n classic Application Load Balancer](/load-balancing/docs/https/setting-up-http-https-redirect)\n\nThis example shows how to use [URL map\nredirects](/load-balancing/docs/url-map-concepts#url-redirects) to redirect all\nrequests from HTTP to HTTPS. This example shows how you set up\nredirects by using the well-known ports 80 (for HTTP) and 443 (for HTTPS).\nHowever, you're not required to use these specific port numbers. Each forwarding\nrule for an Application Load Balancer can reference a [single port from\n1-65535](/load-balancing/docs/forwarding-rule-concepts#port_specifications).\n\nHTTPS uses TLS (SSL) to encrypt HTTP requests and responses, making it\nsafer and more secure. A website that uses HTTPS has `https://` in the beginning\nof its URL instead of `http://`.\n\nFor new HTTPS load balancers\n----------------------------\n\nRegional external Application Load Balancers don't support creating HTTP-to-HTTPS redirects\nwhile creating a new load balancer. You'll need to first use the instructions\nfrom the [Compute Engine backend\nguide](/load-balancing/docs/https/setting-up-reg-ext-https-lb) to create a new\nload balancer. You can then use the instructions in the next section to set\nup a redirect for all requests from HTTP to HTTPS.\n\nThis procedure assumes that you already have an external HTTPS load balancer\nthat is serving HTTPS traffic on port 443.\n\nFor existing load balancers\n---------------------------\n\nIf you already have an HTTPS Application Load Balancer (called here\n*LB1* ) that is serving HTTPS traffic on port 443, you must create a partial\n*HTTP* Application Load Balancer (called here *LB2*) with the following setup:\n\n- The same frontend IP address used by LB1\n- A redirect configured in the URL map\n\nThis partial HTTP load balancer uses the same IP address as your HTTPS load\nbalancer and redirects HTTP requests to your load balancer's HTTPS frontend.\n\nThis architecture is shown in the following diagram.\n[](/static/load-balancing/images/http-to-https-redirect.svg) **Figure 2.** HTTP-to-HTTPS redirect configuration architecture (click to enlarge).\n\n### Redirecting traffic to your HTTPS load balancer\n\nAfter you have verified that your HTTPS load balancer (LB1) is working,\nyou can create the partial HTTP load balancer (LB2) with its frontend\nconfigured to redirect traffic to LB1.\n\nThis example uses the 301 response code. You can instead use a different\n[response code](/load-balancing/docs/https/traffic-management#response-codes).\n\nTo configure the redirect with `gcloud`, you must import a YAML file and make\nsure that your target HTTP proxy points to the URL map that redirects\ntraffic. If you're using the Google Cloud console, this is handled for you.\n\nRegional external Application Load Balancers aren't supported in the Google Cloud console. \n\n### gcloud\n\n1. Create a YAML file `/tmp/web-map-http.yaml`. This example uses MOVED_PERMANENTLY_DEFAULT as the response code. \n\n```yaml\n kind: compute#urlMap\n name: web-map-http\n defaultUrlRedirect:\n redirectResponseCode: MOVED_PERMANENTLY_DEFAULT\n httpsRedirect: True\n tests:\n - description: Test with no query parameters\n host: example.com\n path: /test/\n expectedOutputUrl: https://example.com/test/\n expectedRedirectResponseCode: 301\n - description: Test with query parameters\n host: example.com\n path: /test/?parameter1=value1¶meter2=value2\n expectedOutputUrl: https://example.com/test/?parameter1=value1¶meter2=value2\n expectedRedirectResponseCode: 301\n \n```\n2. Create the HTTP load balancer's URL map by importing the YAML file. The name for this URL map is `web-map-http`. \n\n```\n gcloud compute url-maps import web-map-http \\\n --source /tmp/web-map-http.yaml \\\n --region=REGION\n \n```\n3. If you are updating an existing URL map, the following prompt appears: \n\n```\n Url Map [web-map-http] will be overwritten.\n\n Do you want to continue (Y/n)?\n \n```\n4. To continue, press **Y**.\n5. Verify that the URL map is updated. Your HTTP load balancer's URL map should look something like this: \n\n```\n gcloud compute url-maps describe web-map-http \\\n --region=REGION\n \n``` \n\n```yaml\n creationTimestamp: '2020-03-23T10:53:44.976-07:00'\n defaultUrlRedirect:\n httpsRedirect: true\n redirectResponseCode: MOVED_PERMANENTLY_DEFAULT\n fingerprint: 3A5N_RLrED8=\n id: '2020316695093397831'\n kind: compute#urlMap\n name: web-map-http\n selfLink: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/regions/\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e/urlMaps/web-map-http\n \n```\n6. Create a new target HTTP proxy or update an existing target HTTP proxy, using `web-map-http` as the URL map. \n\n```\n gcloud compute target-http-proxies create http-lb-proxy \\\n --url-map=web-map-http \\\n --region=REGION\n \n```\nOR \n\n```\n gcloud compute target-http-proxies update http-lb-proxy \\\n --url-map=web-map-http \\\n --region=REGION\n \n```\n7. Create a forwarding rule to route incoming requests to the proxy. The `--address` flag specifies `lb-ipv4-1`, which is the same IP address used for the external HTTPS load balancer. \n\n```\n gcloud compute forwarding-rules create http-content-rule \\\n --load-balancing-scheme=EXTERNAL_MANAGED \\\n --address=lb-ipv4-1 \\\n --network-tier=STANDARD \\\n --region=REGION \\\n --target-http-proxy=http-lb-proxy \\\n --target-http-proxy-region=REGION \\\n --ports=80\n \n```\n\nTesting the HTTP-to-HTTPS redirect\n----------------------------------\n\nNote the reserved IP address that you are using for both load balancers. \n\n```\ngcloud compute addresses describe lb-ipv4-1 \n\n --format=\"get(address)\" \n\n --region=REGION\n```\n| **Important:** If **LB1** and **LB2** don't have the same IP address, the redirect won't work. In this case, [delete](/sdk/gcloud/reference/compute/forwarding-rules/delete) the forwarding rule in **LB2** (HTTP load balancer) and create a new one that uses the same IP address as the forwarding rule in **LB1** (HTTPS load balancer).\n\nIn this example, assume that the reserved IP address is `34.98.77.106`. The\n`http://34.98.77.106/` URL redirects to `https://34.98.77.106/`.\n\nAfter a few minutes have passed, you can test this by running the following\n`curl` command. \n\n```\ncurl -v http://hostname.com\n```\n\nSample output: \n\n```\n* Connected to 34.98.77.106 (34.98.77.106) port 80 (#0)\n\u003e GET / HTTP/1.1\n\u003e Host: hostname.com\n\u003e User-Agent: curl/7.64.0\n\u003e Accept: */*\n\u003e\n\u003c HTTP/1.1 301 Moved Permanently\n\u003c Cache-Control: private\n\u003c Content-Type: text/html; charset=UTF-8\n\u003c Referrer-Policy: no-referrer\n\u003c Location: https://hostname.com\n\u003c Content-Length: 220\n\u003c Date: Fri, 30 Jul 2021 21:32:25 GMT\n\u003c\n\u003cHTML\u003e\u003cHEAD\u003e\u003cmeta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"\u003e\n\u003cTITLE\u003e301 Moved\u003c/TITLE\u003e\u003c/HEAD\u003e\u003cBODY\u003e\n\u003cH1\u003e301 Moved\u003c/H1\u003e\nThe document has moved\n\u003cA HREF=\"https://hostname.com\"\u003ehere\u003c/A\u003e.\n\u003c/BODY\u003e\u003c/HTML\u003e\n* Connection #0 to host hostname.com left intact\n```\n\nRelated procedures\n------------------\n\n- To use Terraform, see the [HTTP-to-HTTPS\n redirect tab](/load-balancing/docs/https/ext-http-lb-tf-module-examples#http-to-https-redirect).\n\n- For GKE, see the [HTTP-to-HTTPS redirects in the\n GKE\n documentation](/kubernetes-engine/docs/how-to/ingress-features#https_redirect).\n\n- For internal Application Load Balancers, see\n [Setting up HTTP-to-HTTPS redirect for\n internal Application Load Balancers](/load-balancing/docs/l7-internal/setting-up-http-to-https-redirect).\n\n- For other types of redirects, see [URL\n redirects](/load-balancing/docs/url-map-concepts#url-redirects).\n\nWhat's next\n-----------\n\n- [Clean up the load balancer setup](/load-balancing/docs/cleaning-up-lb-setup)."]]