首页 > 解决方案 > How to hide my API connection between microservices using Docker?

问题描述

I'm new with Docker and I'm setting up a new application where I have 2 services on my docker-compose file:

# Contains all my API servers

api_load_balancer:  
    build: ./microservices/load_balancer
    restart: always
    ports:
    - "8080:80"

# Contains all my client servers

server_client:
    build: ./microservices/client
    ports:
    - "80:80"
    ....

My microservices/load_balancer nginx.conf looks like this:

events { worker_connections 1024; }
http{
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  upstream api_nodes {
   server api_1:9000;
  }
  upstream socket_nodes {
    ip_hash;
    server socketio_1:5000;
  }


 # SERVER API
 server {
   location /socket.io/ {
     proxy_set_header Access-Control-Allow-Origin *;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $host;

     proxy_pass http://socket_nodes;

     # enable WebSockets
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
  }
  location /api/ {
    proxy_set_header Access-Control-Allow-Origin *;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;

    proxy_pass http://api_nodes;
  }
 }
}

My load_balancer/Dockerfile looks like this:

FROM nginx:alpine
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./ssl ./etc/nginx/ssl
EXPOSE 80

When I try to connect from my client I'm able to do it from my client's server (using the api_load_balancer connection string as they're in the same docker network), but, when I try to do a call from the browser I need to change my connection string to something like localhost:8080 or some.ip.in.public.server:8080.

I don't like the idea of exposing neither my port nor my API configuration like that, so is there any way that I can implement a more transparent connection between those microservices? I don't know if it's even possible to do so.

标签: dockernginxdocker-composemicroservices

解决方案


推荐阅读