Wordpress Multisite - infinite redirects

1. Caddy version (caddy version):

❯ docker-compose exec caddy caddy version
v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=

2. How I run Caddy:

❯ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                                NAMES
87a9477c91a2   caddy:2-alpine           "caddy run --config …"   4 minutes ago    Up 4 minutes    0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 2019/tcp   caddy
c4bd8cc45531   wordpress:5.8.2-apache   "docker-entrypoint.s…"   53 minutes ago   Up 53 minutes   0.0.0.0:8081->80/tcp                                 wordpress
2271e376de05   mysql:5.7                "docker-entrypoint.s…"   53 minutes ago   Up 53 minutes   3306/tcp, 33060/tcp                                  wordpress-mysql

a. System environment:

❯ uname -a; docker --version
Linux whau 5.10.0-8-amd64 #1 SMP Debian 5.10.46-4 (2021-08-03) x86_64 GNU/Linux
Docker version 20.10.5+dfsg1, build 55c4c88

b. Command:

❯ docker-compose up

c. Service/unit/compose file:

❯ cat docker-compose.yml
version: "3"

services:
  caddy:
    container_name: caddy
    image: caddy:2-alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./conf/caddyfile:/etc/caddy/Caddyfile:ro
      - ./data/config:/config
      - ./data:/data
      - ./site:/srv
    networks:
      - caddy

networks:
  caddy:
    external: true

d. My complete Caddyfile or JSON config:

❯ cat conf/caddyfile
{
  email adam@yyy.com
  acme_ca https://acme-v02.api.letsencrypt.org/directory
  #debug
}

wp.shand.net, adam.nz {
  reverse_proxy wordpress:80 {
  }
}

3. The problem I’m having:

I recently moved my WordPress Multisite to a new provider. I’m running WordPress using a Docker container with Caddy as a reverse proxy.

I copied the old WordPress files across to the new setup and restored the MySQL database and things are kind of working.

The Wordpress DOMAIN_CURRENT_SITE is wp.shand.netand the site I’m trying to get to work is adam.nz.

The problem I’m having is that:

If I use http://adam.nz as the “Site Address,” the browser gets redirected to https://adam.nz and the site works (but I get browser “site isn’t secure” errors because the HTML references all the images and CSS as http links).

❯ http adam.nz
HTTP/1.1 308 Permanent Redirect
Connection: close
Content-Length: 0
Date: Thu, 30 Dec 2021 06:56:25 GMT
Location: https://adam.nz/
Server: Caddy

❯ https adam.nz | egrep "adam.nz.*.css" | head
<link rel='stylesheet' id='ugb-style-css-css'  href='http://adam.nz/wp-content/plugins/stackable-ultimate-gutenberg-blocks/dist/frontend_blocks.css?ver=2.17.5' type='text/css' media='all' />
<link rel='stylesheet' id='block-gallery-frontend-css'  href='http://adam.nz/wp-content/plugins/block-gallery/dist/blocks.style.build.css?ver=1.1.6' type='text/css' media='all' />

If I use https://adam.nz as the “Site Address,” I get infinite redirects.

❯ https -f adam.nz
HTTP/1.1 301 Moved Permanently
Cache-Control: max-age=3600
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Thu, 30 Dec 2021 07:01:01 GMT
Expires: Thu, 30 Dec 2021 08:01:02 GMT
Location: https://adam.nz/
Server: Caddy, Apache/2.4.51 (Debian)
X-Powered-By: PHP/7.4.27
X-Redirect-By: redirection

❯ https -F adam.nz
https: error: Too many redirects (--max-redirects=30).

I can’t figure out why I’m getting infinite redirects when I enable https in the “Site Address” and I’m not sure how to debug this.

4. Error messages and/or full log output:

5. What I already tried:

I think what’s happening is that Wordpress isn’t able to match the URL to the multisite “Site Address” and so gets caught in an infinite loop redirecting to itself through Caddy.

But I have no idea how to fix this and I see lots of other people running Caddy plus Multisite without apparent probelms?

Any help would be much appreciated.

Adam.

6. Links to relevant resources:

Okay I think I’ve figured this out. Documenting in case it’s helpful to some poor future soul.

Once I followed the instructions in this post and added the below snippet to my wp-config.php, everything started working.

if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
  $_SERVER['HTTPS'] = 'on';
}

With this snippet it no longer matters whether the “Site Address” is set to http or https, it just works.

It seems like this shouldn’t be required and this should be fixable by making sure that the headers are getting passed through correctly. But I couldn’t figure out any combination of headers that made WP behave as I expected.

1 Like

I’m not sure why WP doesn’t handle this by default, but I have seen that being the solution for others in the past.

:+1:

1 Like

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