如何使用Docker隐藏我的微服务之间的API连接?

时间:2019-02-08 08:13:00

标签: docker nginx docker-compose microservices

我是Docker的新手,我正在设置一个新应用程序,在我的docker-compose文件中有2个服务:

# 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"
    ....

我的微服务/ load_balancer nginx.conf看起来像这样:

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;
  }
 }
}

我的load_balancer / Dockerfile看起来像这样:

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

当我尝试从客户端进行连接时,可以从客户端的服务器进行连接(使用api_load_balancer连接字符串,因为它们位于同一泊坞网络中),但是,当我尝试从客户端进行连接时浏览器我需要将连接字符串更改为localhost:8080或some.ip.in.public.server:8080。

我不喜欢这样既不公开我的端口也不公开我的API配置的想法,那么有什么方法可以实现这些微服务之间更透明的连接?我不知道是否有可能。

0 个答案:

没有答案