Get Wordpress+Apache to work behind nginx reverse-proxy

I'm trying to setup the following:

  • docker container with nginx on localhost as a reverse-proxy for Wordpress, listening on localhost:80. It is also used as a reverse-proxy for other microservices.
  • docker-compose container(s) with Wordpress+Apache, listening on localhost:4261. The docker-compose.yaml was taken from the official example. The 'ports' directive was changed from "8000:80" --> "4261:80".

I tried following numerous guides and troubleshooters, but nginx keeps responding with "111 connection refused". Similar questions (which did not work for me): here and here.

Any ideas?

nginx config:

server {
    # Listen HTTPS
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl; 

    server_name ${NGINX_URL};
    ssl_certificate /certs/certificate.pem;
    ssl_certificate_key /certs/private.key;
    
    location /wp/ {
        rewrite ^/wp/?(.*)$ /$1 break;
        proxy_pass http://localhost:4261/;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
    ...
}

wp-config.php:

...
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
// see also https://wordpress.org/support/article/administration-over-ssl/#using-a-reverse-proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}
...

Your nginx is trying to connect to localhost inside the container. You have to either use host networking for the container or you need to link the nginx container to the WordPress container.

In the docker-compose file that would be:

external_links:
  - wordpress:wordpress

Then you can use

proxy_pass http://wordpress/

in your nginx config. You don't need to expose any port in the WordPress container.