Docker Nginx Proxy: how to route traffic to different container using path and not hostname

穿精又带淫゛_ 提交于 2020-11-30 02:21:27

问题


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

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

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

all must work on port 80.

  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?

回答1:


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.

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

  1. Inside your poject folder create another folder - "vhost.d".
  2. Create file "whoami.local" with custom nginx configuration inside "vhost.d" folder. This file must have the same name as VIRTUAL_HOST!

./vhost.d/whoami.local

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

location /app2 {
  proxy_pass http://app2:8000;
}
  1. Create docker-compose.yml file.

./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. Run docker-compose up
  2. Check configuration

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

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

Result:

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



回答2:


Just use nginx image to create container,**do remember set net "host" **which will make your container share same address and port with host machine.mount nginx.conf file and config proxy table.for example:

docker command:

docker run --name http-proxy -v /host/nginx.conf:/etc/nginx/nginx.conf --net host -itd --restart always nginx

nginx.conf:

server {
  listen 80;
  location /app1 {
    proxy_pass YOUR_APP1_URL;
  }
  location /app2 {
    proxy_pass YOUR_APP2_URL;
  }
}



回答3:


Here is a full nginx.conf

It redirects all to root, and only /api to a different container.

Source and an example container using it

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;

events {
    worker_connections  1024;
}


http {
  server {
        listen 80;
        location / {
            proxy_pass http://frontend:3000/;
        }
        location /api {
            proxy_pass http://backend/api;
    }
  }
}



回答4:


just put this inside /etc/nginx/nginx.conf

    worker_processes  1;

    error_log  /var/log/nginx/error.log;

    events {
        worker_connections  1024;
    }


    http {
      server {
            listen 80;

            location /api {
                proxy_pass http://awesome-api;
                proxy_redirect     off;
                proxy_set_header   Host $host;
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Host $server_name;
            }
      }
    }




回答5:


Default bridge network has gateway on 172.17.0.1. You can use this IP address in your nginx.conf

server {
    listen 80;
    server_name example.com;

    location /app1 {
        proxy_pass  http://172.17.0.1:81;
    }
    location /app2 {
        proxy_pass  http://172.17.0.1:82;
    }
}

They will be accessible using port 80 from outside

You can check your bridge gateway IP address by running command docker network inspect bridge



来源:https://stackoverflow.com/questions/39514293/docker-nginx-proxy-how-to-route-traffic-to-different-container-using-path-and-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!