如何在Docker中设置简单的反向代理? [英] How to setup a simple reverse proxy in docker?

查看:496
本文介绍了如何在Docker中设置简单的反向代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手.我有在多个容器上运行的应用程序.现在,我想发布我所有的应用程序.我计划要做的是创建一个包含我所有应用程序的集群.我想要至少4个容器.

I am new in docker. I have of applications running on multiple container. Now, I would like to publish all my apps. What I am planning to do is do make a cluster containning all my application. I want at least 4 containers.

  1. 像反向代理一样面向Internet的Nginx容器.他负责将流量重定向到其他容器,因为无法通过Internet直接访问这些容器.
  2. 在nodejs中发布网络的Node_js容器( http://www.node-app.me ).
  3. 发布Java EE应用程序的
  4. java_EE容器( http://www.java_ee-app.me ).
  5. 发布Django应用程序的Django容器( http://www.django-app.me ).
  1. Nginx container that is facing internet like a reverse proxy. He is responsible to redirect traffic to other containers, since there are not directly accessible through internet.
  2. Node_js container that publishes a web in nodejs (http://www.node-app.me).
  3. java_EE container that publishes Java EE application (http://www.java_ee-app.me).
  4. Django container that publishes a Django application(http://www.django-app.me).

这是我的主意,但是我不知道如何设置nginx容器以扮演代理角色并将请求发送到正确的容器,以便用户发送类似

This is the idear I have, but I don't no how to set nginx container to play the proxy role and make the request to the correct container so that if user send a request like http://www.node-app.me, the container nginx will return result from Node_js, and so on. Can you please give idear on where to start ?

设置看起来像这样(对不起,我不太擅长绘图):

The setup could look like this (sorry I am not very good at drawing) :

推荐答案

除非您对nginx有特殊需要,否则建议您使用 Træfik做反向代理.可以将其配置为通过容器上的标签动态选择反向代理规则.这是一个基本示例.

Unless you have a specific need for nginx, I suggest you use Træfik to do the reverse proxy. It can be configured to dynamically pick up reverse proxy rules via labels on your containers. Here's a basic example.

首先,为Træfik和您的三个容器创建一个公共网络.

First, create a common network for Træfik and your three containers.

docker network create traefik

运行Træfik并暴露端口80,并启用docker后端.

Run Træfik with port 80 exposed and the docker backend enabled.

docker run --name traefik \
-p 80:80 \
-v /var/run/docker.sock:/var/run/docker.sock \
--network traefik \
traefik:1.2.3-alpine \
--entryPoints='Name:http Address::80' \
--docker \
--docker.watch

运行带有适当标签的三个服务.确保他们与Træfik共享一个公共网络,以便Træfik可以到达它. node_js可能看起来像这样.

Run your three services with the appropriate labels. Make sure they share a common network with Træfik so that Træfik can reach it. The node_js one might look something like this.

docker run --name node_js \
--network traefik \
--label 'traefik.frontend.rule=Host:www.node-app.me' \
--label 'traefik.frontend.entryPoints=http' \
--label 'traefik.port=80' \
--label 'traefik.protocol=http' \
your_node_js_image

Træfik将动态地创建一个前端规则,该规则在www.node-app.meHost标头上与该容器运行时匹配. traefik.porttraefik.protocol标签使Træfik知道如何与您的容器通信.

Træfik will dynamically create a frontend rule that matches on the Host header for www.node-app.me when it sees this container running. The traefik.port and traefik.protocol labels let Træfik know how to communicate with your container.

有关更多选项和详细信息,请参见Træfik的Docker后端的文档.

See the documentation for Træfik's Docker backend for more options and details.

这篇关于如何在Docker中设置简单的反向代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆