Caddy V2 with Wordpress (both in docker)

1. Caddy version (caddy version):

Latest Caddy docker image

2. How I run Caddy:

Caddy in a docker environment; Latest version keeping up to date with watchtower.

a. System environment:

OS: Currently on Ubuntu 20.04 LTS
Docker: Version 19.03.8

b. Command:

docker start caddy

c. Service/unit/compose file:

docker run -d -p 83:80 -p 446:443 -v /XXXX:/data -v /XXXX:/etc/caddy/Caddyfile --restart=always --name caddy caddy

d. My complete Caddyfile or JSON config:

XXX.com {
 reverse_proxy 192.168.1.161:81
 }
XXX.XXX.com {
 reverse_proxy 192.168.1.161:32400
 }
XXX.XXX.com {
 reverse_proxy 192.168.1.151:3579
}
XXX.XXX.com {
 reverse_proxy 192.168.1.161:9980
}
XXX.XXX.com {
 reverse_proxy 192.168.1.161:88
}
XXX.XXX.com {
 reverse_proxy wordpress:8585
}
XXX.XXX.com {
    redir / /XXX/ 308
    reverse_proxy 192.168.1.161:8080 {
        flush_interval -1
    }
}
XXX.com {
    redir / /XXX/ 308
    reverse_proxy 192.168.1.161:8080 {
        flush_interval -1
    }
}

3. The problem I’m having:

I got Wordpress running in a docker. The site works well if I go to 192.168.1.161:8585. I want to use my Caddy V2 to do the reverse proxy however the config mentioned above doesn’t work.

4. Error messages and/or full log output:

When browsing to my subdomain i get “ERR_CONNECTION_TIMED_OUT”

5. What I already tried:

I attempted multiple alterations of my config on suggestions of what I found on internet; among those:

example.com

root * /var/www/wordpress
php_fastcgi unix//run/php/php-version-fpm.sock
file_server
mywebsite.domain.ext {
        root * /var/www/html
        reverse_proxy * localhost:2346 {
                header_up Host {http.reverse_proxy.upstream.hostport}
                header_up X-Forwarded-For {http.request.remote}
                header_up X-Real-IP {http.reverse-proxy.upstream.address}
                header_up X-Forwarded-Port {http.reverse_proxy.upstream.port}
                header_up X-Forwarded-Proto {http.request.scheme}
        }
}
dev.example.com {
  proxy / wordpress {
    transparent
  }
}
XXX.XXX.com {
    redir / /index.php/ 308
    reverse_proxy 192.168.1.161:8585 {
        flush_interval -1
    }
}

The latest entry of my caddy docker log is this:

{"level":"info","ts":1601016137.9601762,"msg":"serving initial configuration"}

By now I pretty much used any variation of the configuration I could find online and in combinations driving me pretty nuts.
The thing I don’t really understand is the php_fastcgi either; do I need that or is it optional? I am looking to run a really low-demand amount of webpages and using Wordpress to ease the setup.
Are there things I need to change in the Wordpress settings?

Any help would be really appreciated! Thanks in advance!

6. Links to relevant resources:

How are you running WordPress? The image variant you’re using matters here.

If you’re running a variant that has a web server built in (like Apache) then a reverse proxy will work fine.

If you’re running the variant that uses php-fpm, then you’ll need to use php_fastcgi, and for that to work, Caddy will need to share a volume with the WordPress container’s webroot so that Caddy can read the PHP files to execute.

Did not know that!

Running it through docker compose. File here:

wordpress:
    image: wordpress
    container_name: XXX
    links:
     - mariadb:mysql
    environment:
     - WORDPRESS_DB_PASSWORD=XXXXX
    ports:
     - 8585:80
    restart: always
    volumes:
     - /XXXX:/var/www/html
mariadb:
    image: mariadb
    container_name: XXX
    restart: always
    environment:
     - MYSQL_ROOT_PASSWORD=XXXX
     - MYSQL_DATABASE=wordpress
    volumes:
     - /XXXX:/var/lib/mysql

That was the point i was heavy a lack of knowledge. I don’t understand the php_fastcgi part or wether I need it. What your saying might clear that part up a little. I don’t know how to check for that version though and, considering the fact i am no expert, what are the Wordpress versions/images for docker that DO have a web server built in?

When i browse to my 192.168.1.161:8585 it works like a charm though, does that suggest a built in webserver?

Oh, I see the problem - you’re using the wrong port. You should use wordpress:80 to proxy to the WordPress container. Port 80 is how the container is accessible from inside the docker network. Port 8585 is the mapping to the host.

Also, you can add Caddy as a service to your docker-compose file, it’ll be easier to manage that way rather than running it with a docker run command.

Thanks @francislavoie! I adjusted it, restarted caddy without succes unfortantly.

Starting to feel it has to do with the php_fastcgi and web server thingy. Going to take a look into it later today.

Tested multiple docker images from wordpress with/without apache/fast_cgi without succes.

I also tested the apache versions with:

192.168.1.161:8585
wordpress:80
Wordpress:8585

And the fast_cgi also with multiple combinations.

What is the big difference between the wordpress container and any other service i am reverse proxy-ing? I also got plex/home-assistant/bitwarden/nextcloud and a few others which just work with:

reverse_proxy 192.168.1.161:PORT

Really hoping someone got the key suggestion!!

Edit:

Loosing my mind on this.

I got this in my caddy:

reverse_proxy 192.168.1.161:8585

Site: Not available.
BUT: when I went to site/wp-admin/ it DOES load my admin page. Even on my cellular phone to really check it was externally accessible.
I don’t understand this behaviour. Really hoping for some input!

When you try reverse_proxy wordpress:80 what behaviour are you seeing exactly? What does your browser say? Open the network tab, what error code did you get? What’s in your Caddy logs? What’s in your wordpress logs?

Try to get a shell in the Caddy container (with docker exec -it caddy /bin/sh) and try connecting directly to your wordpress container with curl. You’ll likely need to install curl first with apk add curl, then run curl -v wordpress:80. What do you see?

I don’t think the php_fastcgi approach is right here, because the default wordpress image variant is the apache one (ctrl+f for “latest” on Docker Hub and you’ll see that it appears on the line with the apache tags, under the Supported tags and respective Dockerfile links section). This means it comes bundled with a webserver already. If you were using one of the -fpm variants, you would use php_fastcgi.

@Wouter_Verduin If you follow the Quick Reference docker-compose example in the link @francislavoie has referenced, it works perfectly well. The multi-container Docker application it creates includes the latest WordPress along with an Apache web server, MySQL database and PHP scripting language. You will be able to access it with http://host-ip:8080.

To access the application behind a Caddy reverse proxy, the Caddyfile code block below will suffice.

example.com {
  reverse_proxy http://host-ip:8080
}

That’s if Caddy is running on the host @basil but he’s running it in a docker container, hence wordpress:80.

Yes, I see your point. Would that not be true though only if the Caddy image were included in the multi-container application? I got the feeling, that a separate Caddy application based on the Docker Caddy image was being used in conjunction with the multi-container WordPress application.

@basil and @francislavoie Thanks for your time and input!

I got it running; thanks to your suggestions, but most importantly i figured out where i f****d up.

What I ended up doing was setting up the docker for Wordpress; checking it through the local IP and port, configure about to see if it was working somewhat and edit some pages but mostly tear some down to start from scratch.
After that I started setting up the reverse proxy in caddy. For some reason (unknowingly) i removed the landing page but through local IP it got memorized but leading to a timeout when I browsed to it from my domain name. I came to this when I went to XXX.mydomain.com/wp-admin/ and it DID show, leading me to conclude the problem was in Wordpress and NOT caddy. Removed EVERYTHING I had from Wordpress, started my Wordpress container from scratch and completely ignored setting it up through local IP and just directly from the domain address method.
I still don’t completely understand how this went but to test the process i made an entire new site/reverse proxy every step i intended it the first time and it works like a charm…

For future reference i will leave my final setup here:

Wordpress in container with this docker-compose, edit to need:

wordpress:
    image: wordpress
    container_name: XXX
    links:
     - mariadb:mysql
    environment:
     - WORDPRESS_DB_PASSWORD=XXXXX
    ports:
     - 8585:80
    restart: always
    volumes:
     - /XXXX:/var/www/html
mariadb:
    image: mariadb
    container_name: XXX
    restart: always
    environment:
     - MYSQL_ROOT_PASSWORD=XXXX
     - MYSQL_DATABASE=wordpress
    volumes:
     - /XXXX:/var/lib/mysql

And in my caddyfile:

something.example.com {
  reverse_proxy IP-of-docker:8585
}

Then: Directly go to something.example.com and start editing your site; dont bother with configuring through local IP to avoid conflict or confusion.

Once again thanks for the time and help!

This topic was automatically closed after 30 days. New replies are no longer allowed.