You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Traefik与Ghost部署端口映射失效问题排查求助

Ghost博客Traefik反向代理Bad Gateway问题排查与解决

问题描述

使用Dokploy通过Docker Compose搭配Traefik反向代理部署Ghost博客时,遇到以下异常:

  • 直接访问Ghost默认的2368端口时服务正常
  • 配置端口映射3204:2368后,通过Traefik反向代理访问出现Bad Gateway错误

核心问题分析

你的配置存在两个关键错误:

1. Traefik服务端口配置错误

Traefik与Ghost容器处于同一个dokploy-network Docker网络中,容器间通信应直接使用容器内部端口(2368),而非宿主机的映射端口(3204)。当前Traefik标签中loadbalancer.server.port设置为3204,导致Traefik尝试访问Ghost容器未监听的端口,从而触发Bad Gateway。

2. MySQL环境变量格式错误

ghst_db服务的environment配置写法不规范,可能导致Docker Compose解析异常,间接影响Ghost服务启动。正确格式需使用键值对(冒号分隔)。

修正后的配置

name: ghost-live-1
services:
  ghost:
    image: ghost:5-alpine
    container_name: ghost_serv_live_k
    restart: always
    environment:
      database__client: mysql
      database__connection__host: ghst_db
      database__connection__user: ghost
      database__connection__password: ghost
      database__connection__database: ghost
      url: https://blog.mydomain.com
      NODE_ENV: production
    # 可选:移除宿主机端口映射,Traefik已完成反向代理,无需直接暴露端口
    # ports:
    #   - '3204:2368'
    labels:
      - traefik.docker.network=dokploy-network
      - traefik.enable=true
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-web.rule=Host(`blog.mydomain.com`)
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-web.entrypoints=web
      # 修正为容器内部端口2368
      - traefik.http.services.ghostlive-ghost-2e1bbd-50-web.loadbalancer.server.port=2368
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-web.service=ghostlive-ghost-2e1bbd-50-web
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-web.middlewares=redirect-to-https@file
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-websecure.rule=Host(`blog.mydomain.com`)
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-websecure.entrypoints=websecure
      # 修正为容器内部端口2368
      - traefik.http.services.ghostlive-ghost-2e1bbd-50-websecure.loadbalancer.server.port=2368
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-websecure.service=ghostlive-ghost-2e1bbd-50-websecure
      - traefik.http.routers.ghostlive-ghost-2e1bbd-50-websecure.tls.certresolver=letsencrypt
    volumes:
      - ghost_serv:/var/lib/ghost/content
    depends_on:
      ghst_db:
        condition: service_healthy
    networks:
      - dokploy-network
  ghst_db:
    image: mysql:9
    container_name: ghost_db_live_k
    restart: always
    environment:
      # 修正为标准键值对格式
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: ghost
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    networks:
      - dokploy-network
    volumes:
      - ghost_db:/var/lib/mysql
    healthcheck:
      test:
        - CMD
        - mysqladmin
        - ping
        - '-h'
        - 127.0.0.1
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 60s
volumes:
  ghost_serv:
    driver: local
  ghost_db:
    driver: local
networks:
  dokploy-network:
    external: true

后续操作步骤

  1. 停止当前运行的容器:docker-compose down
  2. 启动修正后的配置:docker-compose up -d
  3. 检查容器状态:docker-compose ps,确保Ghost和MySQL容器均正常运行
  4. 查看Traefik日志验证连接状态:docker logs <traefik-container-name>

内容的提问来源于stack exchange,提问作者Shah-G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:23:15