Traefik搭配Nextcloud出现Bad Gateway错误的排查求助
Let's break down why you're hitting that bad gateway error when accessing your Nextcloud instance through Traefik—there's a critical misconfiguration in your Nextcloud container's port setup:
1. The Core Issue: Wrong Port Target for Nextcloud
You’ve set Traefik to route traffic to port 9000 for your Nextcloud app via the traefik.port=9000 label, but here’s the key problem: Nextcloud’s official image uses port 9000 for PHP-FPM, not a standard HTTP web server. Traefik is a reverse proxy that speaks HTTP, so trying to proxy to a PHP-FPM port will fail every time—this is exactly why you’re seeing the bad gateway error.
The correct HTTP port for Nextcloud’s official image is 80 (this is where the built-in web server listens for requests).
2. Fix the Nextcloud Docker Compose Config
Update your Nextcloud service section to point Traefik to the right port, and optionally add a local port mapping for direct testing without Traefik:
version: '2.1' volumes: nextcloud: db: services: db: image: mariadb volumes: - db:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=my_root_password - MYSQL_PASSWORD=my_password - MYSQL_DATABASE=my_database - MYSQL_USER=my_user app: image: nextcloud restart: always networks: - web - default # Optional: Local port mapping to test Nextcloud directly ports: - "127.0.0.1:8080:80" labels: - "traefik.backend=app" - "traefik.docker.network=web" - "traefik.frontend.rule=Host:my_host" - "traefik.enable=true" # Change this from 9000 to 80 - "traefik.port=80" - "traefik.default.protocol=http" networks: web: external: true
3. Additional Checks to Rule Out Other Problems
Network Connectivity: Confirm both the Nextcloud and Traefik containers are attached to the
webexternal network. Verify this with:docker network inspect webLook for both containers listed in the "Containers" section of the output.
Traefik ACME File Permissions: Your
acme.jsonfile needs strict 600 permissions for Traefik to manage SSL certificates correctly. Run this if you haven’t already:chmod 600 /opt/traefik/acme.jsonCheck Logs for Clues: If issues persist, dig into the container logs to spot other errors (like database connection problems):
# Nextcloud service logs docker-compose logs app # Traefik service logs cd /path/to/your/traefik-compose-folder docker-compose logs traefik
4. Restart Services to Apply Changes
After updating the configs, restart both service stacks to make the changes take effect:
# Restart Nextcloud and MariaDB docker-compose up -d # Restart Traefik cd /path/to/your/traefik-compose-folder docker-compose up -d
Once you’ve made these tweaks, Traefik will correctly proxy HTTP requests to Nextcloud’s web server port, and you should be able to access your instance without the bad gateway error.
内容的提问来源于stack exchange,提问作者Daniel McCrevan

