0

i have 2 nginx container , which i have create via docker-compose file. following is my docker-compose file.

version: '3.7'

services:
  nginx1:
    image: nginx:latest
    hostname: nginx1
    container_name: nginx1
    restart: on-failure
    volumes:
      - nginx1-web:/usr/share/nginx/html
      - nginx1-conf:/etc/nginx
    ports:
      - "81:80"
    networks:
      - kong-net

  nginx2:
    image: nginx:latest
    hostname: nginx2
    container_name: nginx2
    restart: on-failure
    volumes:
      - nginx2-web:/usr/share/nginx/html
      - nginx2-conf:/etc/nginx
    ports:
      - "82:80"
    networks:
      - kong-net

networks:
  kong-net:

volumes:
  kongconfig:
  nginx1-web:
  nginx1-conf:
  nginx2-web:
  nginx2-conf:

my host machine IP is 1.1.1.1 i have added DNS against this IP for domain api. abc.com

then i have create a file with name "nginxfile" in "/var/nginx/sites-available" for reverse proxy. then created its symbolic link. following is the content of file nginxfile

server {
    listen 80;
    server_name api.abc.com;


    location /nginx1 {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://localhost:81;
        proxy_redirect off;
    }

    location /nginx2 {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://localhost:82;
        proxy_redirect off;
    }   

}

now when I open 1.1.1.1:81 or 1.1.1.1:82 in browser it works prefect. but when I tried to access with api.abc.com/nginx1 and api.abc.com/nginx2 it gives me "404 Not Found" error.

I am not sure where is the mistake. please help me in troubleshoot it.

I actually want that when I open api.abc.com/nginx1 then container 1 will be open and when I open api.abc.com/nginx2 then container 2 will be open.

M Usama
  • 60
  • 1
  • 5

0 Answers0