Nginx Reverse-Proxy configuration with Docker Containers

  configuration, docker, nginx, reverse-proxy

since few days I play around with NGINX and Docker. I try to configure the NGINX as Proxy for other services, but I got stacked. Let me explain my problem. as first I have several Docker container running on a Ubuntu Server 20 LTS together configured in a custom network called services.

docker network create -d bridge --subnet=172.18.0.0/16 services

Then I run a container for MySQL, PhpMyAdmin, Apache2/PHP7 and Nginx. Here are the commands how I created the containers:

docker run -d --name mysql --restart=always 
--net services --ip 172.18.0.3 
-e MYSQL_ROOT_PASSWORD=xxxx 
-v /home/mysql/data:/var/lib/mysql 
-v /home/mysql/conf:/etc/mysql 
mysql:5.7.32

docker run -d --name phpmyadmin --restart=always 
-p 8000:80 --net services --ip 172.18.0.2 
-e PMA_HOST=172.18.0.3 
-e MYSQL_ROOT_PASSWORD=xxxx 
-v /home/sql:/etc/phpmyadmin/sql 
-v /home/phpMyAdmin.config.inc.php:/etc/phpmyadmin/config.user.inc.php 
phpmyadmin/phpmyadmin:4.8

docker run -d --name lamp-7.4 --restart=always 
-p 8002:80 -p 587 -p 25 -p 465 -p 443 -p 3306 --net services --ip 172.18.0.99 
-v /home/htdocs:/var/www/html 
-v /home/apache:/etc/apache2 
-v /home/php:/usr/local/etc/php 
php:7.4.15-apache

docker run -d --name nginx --restart=always 
-p 80:80 -p 587 -p 25 -p 465 -p 443 -p 8002 -p 8000 -p 8090 --net services --ip 172.18.0.100 
-v /home/nginx:/etc/nginx/conf.d 
nginx:1.19.7

Every container is working fine, as long I connect direct by the Server IP (not the container IP). Lets say this I is: 213.13.9.121. On the Apache Server running 2 WordPress installations called site and site2. That means I reach those Installations by the URI

The PhpMyAdmin is reachable by the URI:

Now I have my domain sample.org is reachable by the Nginx container. My Nginx config file default.config:

server {
    listen       80;
    listen  [::]:80;
    server_name  sample.org www.sample.org;
 
    charset UTF-8;
    gzip on;
    
    access_log off;
    rewrite_log on;
    error_log /var/log/nginx/error.log error;

    location /dba/ {
        proxy_buffering off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://213.13.9.121:8000/;    
        proxy_read_timeout 60;
    }
}

I’m no able to find a configuration the Services are reachable by he following URI:

I already opened all defined ports in my providerss (IONOS, Cloud Server) configuration and the Domain Cache is set to 1 Minute.

Each variation of the proxy configuration I tried was not working well. In the best case I was able to see for http://sample.org/dba/ the login page, but after login I got a redirect to sample.org without the path /dba/. after I added this manually it was working. After playing around some days without any success I’m desperate and also a bit lost. Because if I made the correct redirection then I need to enable SSL. The certificates I got from my provider I really hope to find here some help. Thanks a lot in advanced.

Source: Docker Questions

LEAVE A COMMENT