如何实现多域名关联单个GCS存储桶?官方文档规则存疑
You’re right that GCS enforces bucket names to match the primary domain for static website hosting, but don’t worry—there are two reliable ways to get multiple domains serving content from the same bucket:
Method 1: Use Cloud Load Balancer (Recommended for Seamless Serving)
This approach lets all your domains display the same GCS content directly (no redirects) by routing traffic through a load balancer that acts as a middle layer.
Step 1: Set up your primary GCS bucket
Create a bucket named after your main domain (e.g.,example.com) and upload all your content here. Enable static website hosting if you’re serving a static site.Step 2: Create a Cloud Load Balancer with a Backend Bucket
- Go to the Cloud Console’s Load Balancing page and create a new HTTP(S) load balancer.
- Choose the "From Internet to my VMs or serverless services" configuration.
- For the backend, select "Backend bucket" and link it to your primary GCS bucket.
- Assign a static external IP address to the load balancer (you’ll need this for DNS setup).
Step 3: Add SSL Certificates for All Domains
Use Google Managed SSL Certificates to cover all your domains:- In the load balancer’s HTTPS proxy setup, create a new managed certificate and list every domain you want to use (e.g.,
example.com,blog.example.com,docs.example.com). - Google will automatically verify and issue the certificate for all listed domains.
- In the load balancer’s HTTPS proxy setup, create a new managed certificate and list every domain you want to use (e.g.,
Step 4: Update DNS Records
For each domain you want to associate:- If using a domain with a DNS provider, create a
CNAMErecord pointing to your load balancer’s static IP’s domain name (or anArecord pointing directly to the static IP).
- If using a domain with a DNS provider, create a
Now all your domains will route through the load balancer and serve content from your single GCS bucket.
Method 2: Domain Redirects (For Simple Use Cases)
If you don’t mind visitors being redirected to your primary domain, this is a lighter-weight option:
Step 1: Create empty buckets for extra domains
For each additional domain (e.g.,blog.example.com), create a GCS bucket with that exact domain name—you don’t need to upload any content here.Step 2: Configure redirects
In each empty bucket’s settings:- Go to the "Website configuration" tab.
- Enable "Redirect all requests to another host name".
- Enter your primary domain (e.g.,
example.com) and choose a redirect type (301 for permanent, 302 for temporary).
Visitors to blog.example.com will automatically be sent to example.com, which serves content from your main bucket.
内容的提问来源于stack exchange,提问作者Rei OKawara

