Docker Nginx 代理:如何使用路径而不是主机名将流量路由到不同的容器 [英] Docker Nginx Proxy: how to route traffic to different container using path and not hostname

查看:19
本文介绍了Docker Nginx 代理:如何使用路径而不是主机名将流量路由到不同的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设现在我在同一服务器上的不同路径上运行了不同的应用程序:

lets say that now I have different app running on the same server on different path:

  • 10.200.200.210/app1
  • 10.200.200.210/app2
  • 10.200.200.210/app3

我想使用 nginx 作为代理在不同的 Docker 容器上运行每个应用程序.

I want to run each app on a different Docker container using nginx as a proxy.

我尝试了 jwilder/nginx-proxy,如果我使用不同的域名(app1.domain.com、app2.domain.com 等),但我无法使用域,我需要使用相同的 IP.

I tried jwilder/nginx-proxy and works great if I use different domain names (app1.domain.com, app2.domain.com, etc), but I'm not able to use domains, I need to use the same IP.

我也不能使用不同的端口,例如:

also I can't use different ports like:

  • 10.200.200.210:81/app1
  • 10.200.200.210:82/app2
  • 10.200.200.210:83/app3

都必须在端口 80 上工作.

all must work on port 80.

  1. 有没有办法配置 jwilder/nginx-proxy 来做到这一点?
  2. 是否有另一个 Docker 镜像,例如 jwilder/nginx-proxy 可以实现.
  3. 或者请你给我一些提示,让我自己构建一个 nginx docker 容器?
  1. Is there a way to configure jwilder/nginx-proxy to do this?
  2. Is there another Docker image like jwilder/nginx-proxy that make it.
  3. or pls could you give me some hint to build an nginx docker container by myself?

推荐答案

如果有人仍在寻找答案.jwilder/nginx-proxy 允许您在代理范围或每个 VIRTUAL_HOST 基础上使用自定义 Nginx 配置.

In case if somebody is still looking for the answer. jwilder/nginx-proxy allows you to use custom Nginx configuration either a proxy-wide or per-VIRTUAL_HOST basis.

这是使用 Per-VIRTUAL_HOST 位置配置的方法.

Here's how can you do it with Per-VIRTUAL_HOST location configuration.

  1. 在您的项目文件夹中创建另一个文件夹 - vhost.d".
  2. 在vhost.d"文件夹中使用自定义 nginx 配置创建文件whoami.local".此文件必须与 VIRTUAL_HOST 同名!

./vhost.d/whoami.local

./vhost.d/whoami.local

location /app1 {
  proxy_pass http://app1:8000;
}

location /app2 {
  proxy_pass http://app2:8000;
}

  1. 创建 docker-compose.yml 文件.

./docker-compose.yml

./docker-compose.yml

version: '3'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
    - "8080:80"
    volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
    - /path/to/vhost.d:/etc/nginx/vhost.d:ro

  gateway:
    image: jwilder/whoami
    environment:
    - VIRTUAL_HOST=whoami.local

  app1:
    image: jwilder/whoami

  app2:
    image: jwilder/whoami

  1. 运行 docker-compose up
  2. 检查配置

在 bash 中运行:

In bash run:

$ curl -H "Host: whoami.local" localhost:8080
I'm 1ae273bce7a4
$ curl -H "Host: whoami.local" localhost:8080/app1
I'm 52b1a7b1992a
$ curl -H "Host: whoami.local" localhost:8080/app2
I'm 4adbd3f9e7a0
$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                  NAMES
6a659a4d4b0a        jwilder/nginx-proxy   "/app/docker-entrypo…"   54 seconds ago      Up 53 seconds       0.0.0.0:8080->80/tcp   nginxreverseproxy_nginx-proxy_1
4adbd3f9e7a0        jwilder/whoami        "/app/http"              54 seconds ago      Up 53 seconds       8000/tcp               nginxreverseproxy_app2_1
52b1a7b1992a        jwilder/whoami        "/app/http"              54 seconds ago      Up 53 seconds       8000/tcp               nginxreverseproxy_app1_1
1ae273bce7a4        jwilder/whoami        "/app/http"              54 seconds ago      Up 53 seconds       8000/tcp               nginxreverseproxy_gateway_1

您还可以将whoami.local"域添加到/etc/hosts 文件中,并直接调用此域.

You can also add "whoami.local" domain to /etc/hosts file and make calls to this domain directly.

/etc/hosts

...
127.0.0.1   whoami.local
...

结果:

$ curl whoami.local:8080
I'm 52ed6da1e86c
$ curl whoami.local:8080/app1
I'm 4116f51020da
$ curl whoami.local:8080/app2
I'm c4db24012582

这篇关于Docker Nginx 代理:如何使用路径而不是主机名将流量路由到不同的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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