Dockerized WordPress/nginx: Connection refused while connecting to upstream

  docker, nginx, php, wordpress

I’m having difficulty setting up WordPress proxied with nginx on Docker. I managed to solve most issues, like rewrites and database import, but When accessing WP admin login panel, it works fine but once I log in, I get 502 Bad Gateway, and Docker’s nginx logs say:

2021/07/20 14:01:25 [error] 24#24: *346 connect() failed (111: Connection refused) while connecting to upstream, client: 10.5.0.130, server: $host, request: "POST /example_com/wp-login.php HTTP/1.1", upstream: "https://172.18.0.4:443/wp-admin/", host: "docker-node-01", referrer: "https://docker-node-01/example_com/wp-login.php"

My docker-compose.yml:

  networks:
    main:
      driver: bridge
 
  services:
    wordpress-nginx:
      image: nginx
      container_name: wordpress-nginx
      hostname: wordpress-nginx
      restart: always
      ports:
        - 80:80
        - 443:443
      networks:
        - main
      extra_hosts:
        - "example.com www.example.com:172.18.0.4"
      volumes:
        - ./data/nginx_conf:/etc/nginx/conf.d
        - ./data/nginx_ssl:/etc/nginx/ssl
 
    wordpress-mysql:
      image: mysql
      container_name: wordpress-mysql
      hostname: wordpress-mysql
      restart: always
      networks:
        - main
      env_file:
        - ./etc/wordpress-mysql.env
      volumes:
        - ./data/dbinit:/docker-entrypoint-initdb.d
        - ./data/dbdata:/var/lib/mysql
 
    example.com:
      image: wordpress
      container_name: example.com
      depends_on:
        - wordpress-mysql
      hostname: example.com
      restart: always
      networks:
        - main
      extra_hosts:
        - "example.com www.example.com:127.0.1.1"
      env_file:
       - ./etc/wordpress-main.env
      volumes:
       - ./data/wp_plugins:/var/www/html/wp-content/plugins
       - ./data/wp_themes:/var/www/html/wp-content/themes
       - ./data/wp_uploads:/var/www/html/wp-content/uploads

My nginx configuration:

server {

listen  443 ssl;
server_name $host;
resolver    127.0.0.11;

ssl_certificate     /etc/nginx/ssl/default.crt;
ssl_certificate_key /etc/nginx/ssl/default.key;

location /example_com/ {

    sub_filter          'www.example.com' '$host/example_com';
    sub_filter          'example.com' '$host/example_com';
    sub_filter_once     off;

    proxy_pass          http://example.com/;
    proxy_redirect      http://example.com/ example_com/;
    proxy_set_header    Host example.com;

    proxy_set_header    Accept-Encoding '';
    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_cookie_path   / /;
    proxy_intercept_errors on;
    error_page 301 302 307 = @handle_redirects;

    # no cache
    proxy_no_cache 1;
    proxy_cache_bypass 1;

}

location @handle_redirects {
    set $saved_redirect_location '$upstream_http_location';
    proxy_pass $saved_redirect_location;
}

location ~ ^([^.?]*[^/])$ {
   try_files $uri @addslash;
}

location @addslash {
   return 301 $uri/;
}

location / {
   try_files $uri $uri/ /index.php$is_args$args;
}

}

I cannot find a solution that would work for me in similar questions.

Source: Docker Questions

LEAVE A COMMENT