Home > Mobile >  Reverse proxy nginx docker container with subdomains
Reverse proxy nginx docker container with subdomains

Time:11-13

I have a docker-compose.yml file with multiple services connected to the same network. the services include nginx exposes port 80, a web app(app1) running on 0.0.0.0:8000, a second web app(app2) running on 0.0.0.0:7000, yet another web app(app3) running on 0.0.0.0:5000 and so on

I can access all the apps from the browser when a go to 0.0.0.0:8000 or 0.0.0.0:5000 etc

I want to access the first app with "http://localhost" from the browser i.e the first web app to be the root. then the others to be subdomains for example; for app2 to be access from this: "http://localhost/app2"

WHAT I HAVE TRIED

I have created a server block for each web app as shown bellow

 >> cat app1.conf
upstream app1 {
        server app1:8000;
}
server {
        # this server listens on port 80
        listen 80 ;
        
        server_name app1;

        # the location / means that when we visit the root url (localhost:80/), we use this configuration
        location / {
                
           proxy_pass http://app1;
        }
       
        
}


now for one of the subdomains i have :

upstream app2 {
        server app2:8000;
}
server {
        # this server listens on port 80
        listen 80 ;
        
        server_name app2;

        # the location / means that when we visit the root url (localhost:80/), we use this configuration
        location /app2 {
                
           proxy_pass http://app2;
        }      
}

Here is my docker-compose.yml file:

version: "3.8"
 services:
  app1:
    image: app1 image
    container_name: app1
    networks:
      - local-network
    ports:
      - "8000:80"
    restart: unless-stopped
    
  app2:
    image: app2 image
    container_name: app2
    networks:
      - local-network
    ports:
      - "7000:80"
    restart: unless-stopped
   
  app3:
    image: app3 image
    container_name: app3
    networks:
      - local-network
    ports:
      - "5000:80"
    restart: unless-stopped
   
  nginx:
    image: nginx:latest
    container_name: nginx
    networks:
      - local-network
    command: nginx -g "daemon off;"
    volumes:
        - ./nginx/sites-available/app1.conf:/etc/nginx/conf.d/sites-available/app1.conf
        - ./nginx/sites-available/app2.conf:/etc/nginx/conf.d/sites-available/app2.conf
        - ./nginx/sites-available/app3.conf:/etc/nginx/conf.d/sites-available/app3.conf
        - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - "80:80"
    restart: unless-stopped
    depends_on:
      - app1
      - app2
      - app3
 networks:
   local-network

in my nginx.conf I including the sites-enabled and manually created symlink of the mounted sites-available configuration files in the nginx container.

On the browser "http://localhost/" works fine but "http://localhost/app1" or app2 or app3 redirects to http://localhost/ There are no errors in the docker logs

What might I be doing wrong?

CodePudding user response:

Here is a quick example, both nginx conf file and docker-compose.yml below:

~/Projects/docker/echo-test $ tree

.
├── docker-compose.yml
└── nginx
    └── app.conf

1 directory, 2 files
~/Projects/docker/echo-test $ cat nginx/app.conf

upstream app1 {
        server echoer1:7777;
}
upstream app2 {
        server echoer2:8888;
}
upstream app3 {
        server echoer3:9999;
}

server {
        listen 80;
        server_name localhost;

        location / {
                proxy_pass http://app1;
        }
        location /app2 {
                proxy_pass http://app2;
        }
        location /app3 {
                proxy_pass http://app3;
        }
}
~/Projects/docker/echo-test $ cat docker-compose.yml

version: "3.8"
services:
  echoer1:
    hostname: echoer1
    image: mendhak/http-https-echo
    environment:
      - HTTP_PORT=7777

  echoer2:
    hostname: echoer2
    image: mendhak/http-https-echo
    environment:
      - HTTP_PORT=8888

  echoer3:
    hostname: echoer3
    image: mendhak/http-https-echo
    environment:
      - HTTP_PORT=9999

  nginx:
    image: nginx
    command: nginx -g "daemon off;"
    volumes:
        - ./nginx/app.conf:/etc/nginx/conf.d/app.conf
    ports:
      - "80:80"
  • Related