Traefik代理Mailpit 8025端口返回404问题排查与解决
Traefik反向代理Docker容器(Mailpit示例)的404问题解决
已配置Traefik作为反向代理,whoami服务和Traefik控制台均可正常访问,但添加Mailpit服务后,通过https://localhost/mailpit访问返回404 page not found,直接访问http://localhost:8025则正常,Traefik控制台也能识别到Mailpit服务地址,容器内也可正常访问该地址。
现有Traefik配置
docker-compose.yaml
services: traefik: image: traefik:latest container_name: traefik ports: - "80:80" - "443:443" volumes: - ./htpasswd:/htpasswd - ./traefik.yaml:/etc/traefik/traefik.yaml - /var/run/docker.sock:/var/run/docker.sock labels: - "traefik.http.routers.dashboard.rule=Host(`localhost`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))" - "traefik.http.routers.dashboard.service=api@internal" - "traefik.http.routers.dashboard.tls=true" - "traefik.http.routers.dashboard.middlewares=auth" - "traefik.http.middlewares.auth.basicauth.usersfile=/htpasswd" networks: - traefik whoami: image: traefik/whoami labels: - "traefik.http.routers.whoami.rule=Host(`localhost`) && PathPrefix(`/whoami`)" - "traefik.http.routers.whoami.tls=true" networks: - traefik networks: traefik: external: true
traefik.yaml(静态配置)
api: dashboard: true entrypoints: web: address: ":80" websecure: address: ":443" log: level: TRACE metrics: addInternals: true providers: docker: endpoint: "unix:///var/run/docker.sock"
验证现有配置正常:
执行以下命令可正常返回whoami结果:
$ curl -sk https://localhost/whoami | head Hostname: ea1b0e62d773 IP: 127.0.0.1 IP: ::1 IP: 172.19.0.3 RemoteAddr: 172.19.0.2:37404 GET /whoami HTTP/1.1 Host: localhost User-Agent: curl/8.9.1 Accept: */* Accept-Encoding: gzip
同时可通过https://localhost/dashboard/访问Traefik控制台。
Mailpit初始配置及404问题
添加的Mailpit服务docker-compose.yml:
services: mailpit: image: axllent/mailpit container_name: mailpit restart: unless-stopped volumes: - mailpit-data:/data ports: - 8025:8025 - 1025:1025 environment: MP_MAX_MESSAGES: 5000 MP_DATABASE: /data/mailpit.db MP_SMTP_AUTH_ACCEPT_ANY: 1 MP_SMTP_AUTH_ALLOW_INSECURE: 1 labels: - "traefik.http.routers.mailpit.rule=Host(`localhost`) && PathPrefix(`/mailpit`)" - "traefik.http.routers.mailpit.tls=true" - "traefik.http.routers.mailpit.service=mailpit" - "traefik.http.services.mailpit.loadbalancer.server.port=8025" networks: - traefik volumes: mailpit-data: networks: traefik: external: true
访问测试返回404:
$ curl -sk https://localhost/mailpit | head 404 page not found
但Mailpit本身运行正常:
- 直接访问
http://localhost:8025可打开控制台 - Traefik控制台显示Mailpit服务地址为
http://172.19.0.4:8025 - 在Traefik容器内部可正常访问该地址:
$ docker exec -it traefik wget -O - http://172.19.0.4:8025 | head Connecting to 172.19.0.4:8025 (172.19.0.4:8025) writing to stdout <!DOCTYPE html> <html lang="en" class="h-100"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="referrer" content="no-referrer"> <meta name="robots" content="noindex, nofollow, noarchive">
问题原因
Traefik默认会将请求的完整路径原样转发给后端服务,当访问https://localhost/mailpit时,实际转发给Mailpit的请求路径是/mailpit,但Mailpit的服务根路径是/,并不存在/mailpit这个路由,因此返回404。若使用路径剥离中间件,可能会引发Mailpit内部静态资源加载路径错误等其他问题。
解决方案:使用专属Host
修改Mailpit的docker-compose.yml,移除路径匹配规则,改用专属Host(如mailpit.localhost):
services: mailpit: image: axllent/mailpit restart: unless-stopped volumes: - mailpit-data:/data ports: - 8025:8025 - 1025:1025 environment: MP_MAX_MESSAGES: 5000 MP_DATABASE: /data/mailpit.db MP_SMTP_AUTH_ACCEPT_ANY: 1 MP_SMTP_AUTH_ALLOW_INSECURE: 1 labels: - "traefik.http.routers.mailpit.rule=Host(`mailpit.localhost`)" - "traefik.http.routers.mailpit.tls=true" - "traefik.http.routers.mailpit.service=mailpit" - "traefik.http.services.mailpit.loadbalancer.server.port=8025" networks: - traefik volumes: mailpit-data: networks: traefik: external: true
修改后,通过https://mailpit.localhost即可正常访问Mailpit服务。
内容的提问来源于stack exchange,提问作者dyle
相关产品推荐
相关产品推荐

