resource"google_cloud_run_v2_service""default"{name="cloudrun-service-h2c"location="us-central1"deletion_protection=false # set to "true" in productiontemplate{containers{image="us-docker.pkg.dev/cloudrun/container/hello" # Enable HTTP/2ports{name="h2c"container_port=8080}}}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-25。"],[],[],null,["# Use HTTP/2 for services\n\nWhen a container instance receives requests, Cloud Run downgrades\nthose requests from HTTP/2 to HTTP/1 with the exception of native gRPC traffic,\nwhich requires HTTP/2 to function correctly. This page shows you how to configure\nyour service to use end-to-end HTTP/2, which prevents Cloud Run from\ndowngrading requests to HTTP1.\n\nFor more information about invoking services using HTTP, see\n[Invoking with an HTTPS Request](/run/docs/triggering/https-request).\n\nBefore you configure\n--------------------\n\nYour Cloud Run service must handle requests in\n[HTTP/2 cleartext](https://http2.github.io/faq/#can-i-implement-http2-without-implementing-http11)\n(`h2c`) format. Google's frontend-serving\ninfrastructure terminates TLS and then forwards the `h2c` traffic to Cloud Run\nand to your container through an encrypted channel.\n\nTo confirm that your service supports `h2c` requests,\n[test the service locally](/run/docs/testing/local) using this curl command: \n\n```bash\ncurl -i --http2-prior-knowledge http://localhost:PORT\n```\n\n### Required roles\n\n\nTo get the permissions that\nyou need to configure and deploy Cloud Run services,\n\nask your administrator to grant you the\nfollowing IAM roles:\n\n- [Cloud Run Developer](/iam/docs/roles-permissions/run#run.developer) (`roles/run.developer`) on the Cloud Run service\n- [Service Account User](/iam/docs/roles-permissions/iam#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the service identity\n\n\nIf you are deploying a [service](/run/docs/deploying-source-code#required_roles)\nor [function](/run/docs/deploy-functions#required-roles) from source code, you\nmust also have additional roles granted to you on your project and\nCloud Build service account.\n\nFor a list of IAM roles and permissions that are associated with\nCloud Run, see\n[Cloud Run IAM roles](/run/docs/reference/iam/roles)\nand [Cloud Run IAM permissions](/run/docs/reference/iam/permissions).\nIf your Cloud Run service interfaces with\nGoogle Cloud APIs, such as Cloud Client Libraries, see the\n[service identity configuration guide](/run/docs/configuring/services/service-identity).\nFor more information about granting roles, see\n[deployment permissions](/run/docs/reference/iam/roles#additional-configuration)\nand [manage access](/iam/docs/granting-changing-revoking-access).\n\nSupported HTTP/2 services\n-------------------------\n\nHTTP/2 is ideal for a variety of applications. Some common use cases include\nthe following:\n\n- **gRPC services** : build high-performance, low-latency microservices that communicate using [gRPC](/load-balancing/docs/https#grpc-support).\n- **APIs with high throughput**: reduce overhead for applications that require efficient handling of numerous small requests or need to push data to clients.\n\nSet and update HTTP/2 end-to-end\n--------------------------------\n\nAny configuration change leads to the\ncreation of a new revision. Subsequent revisions will also automatically get\nthis configuration setting unless you make explicit updates to change it.\n\nYou can specify the use of HTTP/2 end-to-end by using the Google Cloud console,\nGoogle Cloud CLI, or YAML when you [create a new service](/run/docs/deploying#service) or\n[deploy a new revision](/run/docs/deploying#revision): \n\n### Console\n\n\n1. In the Google Cloud console, go to Cloud Run:\n\n [Go to Cloud Run](https://console.cloud.google.com/run)\n2. Select **Services** from the menu, and click **Deploy container** to\n configure a new service.\n If you are configuring an existing service, click the\n service, then click **Edit and deploy new revision**.\n\n3. If you are configuring a new service, fill out the initial service\n settings page, then click **Container(s), Volumes, Networking, Security** to expand the\n service configuration page.\n\n4. Click the **Networking** tab.\n\n - Select *Enable http/2 connections*\n5. Click **Create** or **Deploy**.\n\n### gcloud\n\nYou can [update](/sdk/gcloud/reference/run/services/update)\na given service to use HTTP/2 by using the following command: \n\n```bash\ngcloud run services update SERVICE --use-http2\n```\n\nReplace \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your service.\n\nYou can also set your service to use HTTP/2 during\n[deployment](/sdk/gcloud/reference/run/deploy) using the command: \n\n```bash\ngcloud run deploy --image IMAGE_URL --use-http2\n```\n\nReplace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\nexample, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\nthe [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\nalready be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n.\n\n### YAML\n\n1. If you are creating a new service, skip this step.\n If you are updating an existing service, download its [YAML configuration](/run/docs/reference/yaml/v1):\n\n ```bash\n gcloud run services describe SERVICE --format export \u003e service.yaml\n ```\n2. Update `ports` with the name`h2c` and `containerPort` with the port of\n your choice, as shown in the following example:\n\n ```yaml\n apiVersion: serving.knative.dev/v1\n kind: Service\n metadata:\n name: SERVICE\n spec:\n template:\n metadata:\n name: REVISION\n spec:\n containers:\n - image: IMAGE_URL\n ports:\n - name: h2c\n containerPort: 8080\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e: the name of your Cloud Run service\n - \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e .\n - \u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e with a new revision name or delete it (if present). If you supply a new revision name, it **must** meet the following criteria:\n - Starts with \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e`-`\n - Contains only lowercase letters, numbers and `-`\n - Does not end with a `-`\n - Does not exceed 63 characters\n3. Create or update the service using the following command:\n\n ```bash\n gcloud run services replace service.yaml\n ```\n\n### Terraform\n\n\nTo learn how to apply or remove a Terraform configuration, see\n[Basic Terraform commands](/docs/terraform/basic-commands).\nAdd the following to a [`google_cloud_run_v2_service`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service) resource in your Terraform configuration: \n\n\u003cbr /\u003e\n\n resource \"google_cloud_run_v2_service\" \"default\" {\n name = \"cloudrun-service-h2c\"\n location = \"us-central1\"\n\n deletion_protection = false # set to \"true\" in production\n\n template {\n containers {\n image = \"us-docker.pkg.dev/cloudrun/container/hello\"\n # Enable HTTP/2\n ports {\n name = \"h2c\"\n container_port = 8080\n }\n }\n }\n }\n\nIf your container listens for HTTP requests on a port other than `8080`,\nreplace `8080` with that port number.\n\nView http/2 settings\n--------------------\n\nTo view the current http/2 settings for your\nCloud Run service: \n\n### Console\n\n1. In the Google Cloud console, go to Cloud Run:\n\n [Go to Cloud Run](https://console.cloud.google.com/run)\n2. Click the service you are interested in to open the **Service details**\n page.\n\n3. Click the **Revisions** tab.\n\n4. In the details panel at the right, the http/2 setting\n is listed under the **Networking** tab.\n\n### gcloud\n\n1. Use the following command:\n\n ```bash\n gcloud run services describe SERVICE\n ```\n2. Locate the http/2 setting in the returned\n configuration.\n\nUse HTTP/2 behind a load balancer\n---------------------------------\n\nWhen configured to listen for HTTP/2, Cloud Run automatically\nhandles the HTTP/2 or gRPC traffic arriving from the Application Load Balancer. No\nspecific configuration is needed on the load balancer's backend service.\n\nTo configure Cloud Run to use HTTP/2 or gRPC, use one of the\nfollowing methods:\n\n- [Set and update HTTP/2 end-to-end](#setting)\n- Configure an [external Application Load Balancer](/load-balancing/docs/https) with your Cloud Run service as the backend."]]