0
votes

I'm trying to build multiple services and reverse proxy them with nginx.

So service1 is:

http://api/service1 (nginx) => docker (http://service1:4001/) => express (http://localhost:4000)

service2 is :

http://api/service2 (nginx) => docker (http://service2:4002/) => express (http://localhost:4000)

It's my first time experimenting with nginx from scratch and I'm stuck, I can't reach any of my service from http://localhost:80/service1 or http://api/service1. And do you think it's a good start of an architecture for micro-services for dev and production?

I also have doubt about my network inside for my docker compose, is it accurate to put that network or let the default docker network ?

(All of the containers working fine);

docker-compose.yml :

version: '3'

services:
  mongo:
    container_name: mongo
    image: mongo:latest
    ports:
      - '27017:27017'
    volumes:
      - './mongo/db:/data/db'

  nginx:
    build: ./nginx
    container_name: nginx
    links:
      - service1
      - service2
    ports:
      - '80:80'
      - '443:443'
    depends_on:
      - mongo
    networks:
      - api

  service1:
    build: ./services/service1
    container_name: service1
    links:
      - 'mongo:mongo'
    volumes:
      - './services/service1:/src/'
    ports:
      - '4001:4000'
    command: yarn dev
    networks:
      - api

  service2:
    build: ./services/service2
    container_name: service2
    links:
      - 'mongo:mongo'
    volumes:
      - './services/service2:/src/'
    ports:
      - '4002:4000'
    command: yarn dev
    networks:
      - api

networks:
  api:

nginx.conf :

worker_processes 1  ;


events {
  worker_connections  1024;
}

http {

    server {
        listen       80;
        server_name api;
        charset utf-8;

    location /service1 {
        proxy_pass http://service1:4001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /service2 {
        proxy_pass http://service2:4002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    }
}

service DockerFile:

FROM node:latest

RUN mkdir /src
WORKDIR /src

COPY package.json /src/package.json
RUN npm install

COPY . /src/
EXPOSE 4000

nginx DockerFile:

FROM nginx

COPY nginx.conf /etc/nginx/nginx.conf

I'm trying to reach either http://localhost:80/service1/ which would normally get me to http://service1:4001

but I'm getting this error:

[error] 7#7: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server:

172.23.0.1 - - [15/Apr/2020:22:01:44 +0000] "GET / HTTP/1.1" 502 157 "-" "PostmanRuntime/7.24.1" bts-api, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:4001/", host: "localhost:80"

I'm also trying to reach http://api/service1/ (defined in nginx.conf as server_name) but I don't have any response or ping.

2
Your backend services are both listening on port 4000 inside the container, and your nginx config needs to use that port to contact them. The published ports: don't matter (and in fact aren't necessary). - David Maze

2 Answers

0
votes

Please add the proxy_redirect parameter and container access port in your nginx.conf files as below.

server {
    listen       80;
    server_name api;
    charset utf-8;

location /service1 {
    proxy_pass http://service1:4000;
    proxy_redirect      http://service1:4000: http://www.api/service1;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /service2 {
    proxy_pass http://service2:4000;
    proxy_redirect      http://service2:4000: http://www.api/service2;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
0
votes

Finally it was due to my container name being $(project)-$(container-name) so in nginx i replaced accounts => $(project)-accounts