Anand Deshmukh
Anand Deshmukh

Reputation: 1126

How to use NGINX to forward the request to Docker Microservice

I wanted to use NGINX as a reverse proxy to forward the request to microservices. Both NGINX and Microservices are hosted on docker container.

Below is my nginx.conf file

    worker_processes 1;

    events { worker_connections 1024; }

    #test

    http {

        sendfile on;

        # upstream docker-nginx {
        #     server nginx:80;
        # }

        upstream admin-portal {
            # server admin-portal:9006;
            server xx.xx.xx.xx:9006;
            # server localhost:9006;

        }

server {
            listen 8080;

            location / {
                proxy_pass         http://admin-portal;
                proxy_redirect     off;
                proxy_set_header   Host $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-Host $server_name;
            }


        }


    }

Dockerfile

FROM nginx
RUN apt-get update && apt-get install -y \
curl 
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080

docker-compose.yml

version: '3'
services:
  nginx:
    restart: always
    build: ../../conf/
    volumes:
    - ./mysite.template:/etc/nginx/conf.d/mysite.template
    ports:
    - "8080:8080"
    networks:
      - cloud


networks:
  cloud:
   driver: bridge

But if I do localhost:8080/admin-portal/ I am getting below error

nginx_1 | 2018/07/04 07:08:17 [error] 7#7: *1 "/usr/share/nginx/html/admin-portal/index.html" is not found (2: No such file or directory), client: xx.xx.xx.xx, server: your-domain.com, request: "GET /admin-portal/ HTTP/1.1", host: "xx.xx.xx.xx:8080" nginx_1 | 2018/07/04 07:08:17 [error] 7#7: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: xx.xx.xx.xx, server: your-domain.com, request: "GET /admin-portal/ HTTP/1.1", host: "xx.xx.xx.xx:8080" nginx_1 | xx.xx.xx.xx - - [04/Jul/2018:07:08:17 +0000] "GET /admin-portal/ HTTP/1.1" 404 170 "-" "curl/7.47.0" admin-portal/

Please suggest what changes I need to do to forward the request to admin-portal using nginx

Upvotes: 2

Views: 2829

Answers (1)

Thanh Nguyen Van
Thanh Nguyen Van

Reputation: 11772

upstream admin-portal {
    server 127.0.0.1:9006;
}

it should be :

upstream admin-portal {
    server 172.17.0.1:9006;
}

With 172.17.0.1 is ip address gateway of containers.

Or docker inspect containermicroservice_id then get ip address of that container.

 upstream admin-portal {
    server ipaddressofmicroservicecontainer:9006;
}

Put ip address of server into

        server_name    localhost ipaddressofserver www.example.com;

then access http://ipaddressofserver:8080/admin-portal

Comment out this part:

#server {
#            listen       8080;
#            server_name     your-domain.com www.your-domain.com;
#            root   /usr/share/nginx/html;
#            index  index.html index.htm;

#            error_page  404              /404.html;
#            error_page   500 502 503 504  /50x.html;
#            location = /50x.html {
#                root   html;
#            }
        }

Upvotes: 1

Related Questions