Docker: 使用 Traefik 處理自定義域名

本文件是HTTPS 路由指南的一部分。請在此處查看完整指南:如何在 Docker 容器中管理 HTTP 路由和 TLS 加密(HTTPS)

👋 歡迎來到 Stackhero 文件!

Stackhero 提供一個即用型的 Docker cloud CaaS (Containers as a Service) 解決方案,帶來多種優勢,包括:

  • 只需 docker-compose up 即可輕鬆將您的容器部署到生產環境
  • 使用 HTTPS 保護的可自訂域名(例如,https://api.your-company.comhttps://www.your-company.comhttps://backoffice.your-company.com)。
  • 專用私有 VM提供的最佳性能和強大的安全性
  • 只需點擊即可輕鬆更新

節省時間簡化您的生活:只需 5 分鐘即可嘗試 Stackhero 的 Docker CaaS cloud hosting 解決方案,並將您的容器部署到生產環境!

在前面的示例中,我們使用了默認的 <XXXXXX>.stackhero-network.com 域名。實際上,您可能會使用自己的公司或項目域名,例如 www.my-company.comapi.my-project.io。以下部分說明如何配置自定義域名。

在此示例中,您將配置域名 api.my-project.io。將 my-project.io 替換為您擁有的域名,並將 api 替換為您想要的子域名。

首先,更新您的域名 DNS 設置,使 api.my-project.io 指向您的 <XXXXXX>.stackhero-network.com 域名。

  1. 登錄到您的域名提供商並訪問您的 DNS 配置。
  2. 創建一個名為 api(或您選擇的其他子域名)的新條目,將其類型設置為 CNAME,並將其目標設置為 <XXXXXX>.stackhero-network.com

Cloudflare DNS 介面上的 DNS 配置示例Cloudflare DNS 介面上的 DNS 配置示例

DNS 配置完成後,您可以通過運行以下命令進行驗證:

host api.my-project.io

您應該會看到類似的響應:

api.my-project.io is an alias for <XXXXXX>.stackhero-network.com

DNS 傳播可能需要最多 24 小時,具體取決於您的提供商。 如果 host 命令未返回預期的回覆,請稍等片刻再重試。

接下來,使用以下配置更新您的 docker-compose.yml 文件:

services:
  api:
    image: traefik/whoami
    hostname: api
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`api.my-project.io`)" # 在此處添加您的域名
      - "traefik.http.services.api.loadbalancer.server.port=<PORT>" # 將 "<PORT>" 替換為您的 API 所監聽的端口
      - "traefik.http.routers.api.tls.certresolver=letsencrypt"

請不要忘記在 Traefik 標籤 Host 中將 api.my-project.io 替換為您的實際域名。

使用以下命令部署您的容器:

docker-compose up -d

然後訪問 https://api.my-project.io。您應該會看到一個顯示您的容器主機名 api 的文本頁面。

證明 Traefik 正在為我們的新域名處理 HTTP 流量並進行 TLS 加密證明 Traefik 正在為我們的新域名處理 HTTP 流量並進行 TLS 加密

在首次創建容器時,TLS 證書可能需要幾秒鐘才能生成。 如果遇到 TLS 錯誤,請稍等幾秒鐘並刷新頁面,以便證書有時間生成。

恭喜,您現在已配置了您的第一個自定義域名!

在定義網站 URL(例如 my-company.com)時,最好也設置一個 "www" 子域名。這確保通過 www.my-company.com 連接的用戶被重定向到您的主站點,並有助於避免重複內容問題。

在下面的示例中,my-company.comwww.my-company.com 都被處理。訪問 www.my-company.com 的用戶將被重定向到 my-company.com

services:
  frontend:
    image: traefik/whoami
    hostname: frontend
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.frontend.rule=Host(`my-company.com`) || Host(`www.my-company.com`)" # 在此處添加兩個域名
      - "traefik.http.services.frontend.loadbalancer.server.port=<PORT>" # 將 "<PORT>" 替換為您的前端所監聽的端口
      - "traefik.http.routers.frontend.tls.certresolver=letsencrypt"

      # 將 'www.my-company.com' 重定向到 'my-company.com':
      - 'traefik.http.routers.frontend.middlewares=redirect-www'
      - "traefik.http.middlewares.redirect-www.redirectregex.regex=^https://www.my-company.com/(.*)"
      - "traefik.http.middlewares.redirect-www.redirectregex.replacement=https://my-company.com/$${1}"
      - "traefik.http.middlewares.redirect-www.redirectregex.permanent=true"

假設您有一個專門用於文檔網站的容器。您可能希望將 https://my-company.com/docs 路由到此容器,同時將其他請求(例如 https://my-company.com/)發送到您的前端容器。下面的示例顯示了如何完成此操作:

services:
  documentations:
    image: traefik/whoami
    hostname: documentations
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.documentations.rule=Host(`my-company.com`) && PathPrefix(`/docs`)" # 在此處定義路徑前綴
      - "traefik.http.services.documentations.loadbalancer.server.port=<PORT>" # 將 "<PORT>" 替換為您的文檔容器所監聽的端口
      - "traefik.http.routers.documentations.tls.certresolver=letsencrypt"

  frontend:
    image: traefik/whoami
    hostname: frontend
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.frontend.rule=Host(`my-company.com`) || Host(`www.my-company.com`)"
      - "traefik.http.services.frontend.loadbalancer.server.port=<PORT>" # 將 "<PORT>" 替換為您的前端所監聽的端口
      - "traefik.http.routers.frontend.tls.certresolver=letsencrypt"

      # 將 'www.my-company.com' 重定向到 'my-company.com':
      - 'traefik.http.routers.frontend.middlewares=redirect-www'
      - "traefik.http.middlewares.redirect-www.redirectregex.regex=^https://www.my-company.com/(.*)"
      - "traefik.http.middlewares.redirect-www.redirectregex.replacement=https://my-company.com/$${1}"
      - "traefik.http.middlewares.redirect-www.redirectregex.permanent=true"

現在,訪問 https://my-company.com/docs(或任何子路徑,例如 https://my-company.com/docs/something)將顯示來自 documentations 容器的內容。其他路徑,例如 https://my-company.com/help,將由 frontend 容器提供服務。

默認情況下,Traefik 連接到容器的第一個暴露端口。在某些情況下,您可能需要指定特定端口。下面的示例演示如何定義自定義端口:

services:
  frontend:
    image: traefik/whoami
    hostname: frontend
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.frontend.rule=Host(`my-company.com`)"
      - "traefik.http.routers.frontend.tls.certresolver=letsencrypt"

      # 將 'https://my-company.com' 的流量路由到端口 80 上的 'frontend' 容器
      - "traefik.http.services.frontend.loadbalancer.server.port=80"