1

我有一个专用服务器,我想在其上托管不同的站点,每个站点都在一个单独的 docker 容器中运行。我没有域,所以我想通过 IP 地址或 dynDNS URL 访问服务器。每个站点都应该在我的 IPaddress / dyndns 的子文件夹下可用,如下所示:

http://10.10.10.10/site-a --> redirects to e.g. nginx running in container A

http://10.10.10.10/site-b --> redirects to e.g. appache running in container B

等等

我想我应该为此使用反向代理。我发现了这个https://github.com/jwilder/nginx-proxy,如果我打算将来添加更多容器,它在可扩展性方面似乎很容易。但是,如果我尝试直接访问http://10.10.10.10/site-ahttp://10.10.10.10/site-ahttp://10.10.10.10 ,我总是会得到一个 HTTP 503 服务暂时不可用。

我尝试了https://github.com/jwilder/nginx-proxy#docker-compose上描述的 whoami 示例, 如果我尝试 curl -H "Host: whoami.local" localhost,则效果很好

我修改了这个 docker-compose.yml 示例以适合我的用例:

    version: '2'

    services:
      nginx-proxy:
        image: jwilder/nginx-proxy
        ports:
          - "80:80"
        volumes:
          - /var/run/docker.sock:/tmp/docker.sock:ro

      site-a:
        image: nginx
        volumes:
          - /home/chris/docker/site-a:/usr/share/nginx/html
        environment:
          - VIRTUAL_HOST=site-a.local

      site-b:
        image: nginx
        volumes:
          - /home/chris/docker/site-b:/usr/share/nginx/html
        environment:
          - VIRTUAL_HOST=site-b.local

站点 a 和站点 b 只是托管静态 html 文件用于测试目的的 nginx 容器。

执行

curl -H "Host: site-a.local" http://10.10.10.10

从 site-a 返回静态 html

curl -H "Host: site-b.local" http://my-dyn-dns.com

从 site-b 返回静态 html

但是,如果我尝试使用浏览器访问这些 URL 中的任何一个,我会再次收到 HTTP 503 :-(

nginx.conf 未从 dockerhub 映像中更改:

/etc/nginx# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
daemon off;
4

2 回答 2

2

看来您的问题的一部分是您已经配置了基于名称的虚拟主机(这就是为什么curl -H "Host: site-a.local" http://10.10.10.10有效),但您实际上想要基于路径的虚拟主机(这样您就可以访问http://...whatever.../site-a/并获取 site-a.

您绝对可以使用 nginx 进行配置,但您需要使用以下内容添加显式配置nginx.conf

location /site-a/ {
  proxy_pass http://site-a/;
}

有关详细信息以及有关反向代理配置的 nginx 文档,请参阅此答案

如果您愿意为您的前端查看 nginx 以外的东西,这是Traefik的一个很好的用例,它是一个反向代理,旨在与 Docker(和其他提供程序)一起使用。一个关键特性是它根据应用到后端容器的标签动态配置自身。

您可以使用以下内容实现所需的配置docker-compose.yml

---
version: "3"

services:
  site-a:
    image: nginx
    volumes:
      - /home/chris/docker/site-a:/usr/share/nginx/html
    labels:
      traefik.frontend.rule: "PathPrefixStrip:/site-a/"
      traefik.enable: true
      traefik.port: 80

  site-b:
    image: nginx
    volumes:
      - /home/chris/docker/site-b:/usr/share/nginx/html
    labels:
      traefik.frontend.rule: "PathPrefixStrip:/site-b/"
      traefik.enable: true
      traefik.port: 80

  frontend:
    image: traefik
    command: --api --docker --logLevel=DEBUG
    ports:
      - "80:80"

      # Expose the Traefik web UI on port 8080. We restrict this
      # to localhost so that we don't publicly expose the
      # dashboard.
      - "127.0.0.1:8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    labels:
      traefik.enable: false

前端服务无需配置;Traefik 连接到 Docker API 并监视要创建的其他容器,并使用附加到这些容器的标签来配置自身。

于 2019-04-25T12:57:45.433 回答
0

感谢 larsks 的回答,我现在通过使用 nginx-proxy 找到了一个可行的解决方案。我只是使用带有以下 nginx.conf 文件的普通 nginx 容器:

user  nginx;
worker_processes  1;

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

events {
    worker_connections  1024;
}

http {
    server {
      listen 80;
      location /site-a {
        proxy_pass http://site-a/;
      }
      location /site-b {
        proxy_pass http://site-b/;
      }
        # I can even forward to an apache server running on the host on port 81:
      location /site-c/ {
        proxy_pass http://10.10.10.10:81/;
      }
    }
}

我创建了一个 nginx 容器,该容器通过卷链接了本地存储的 nginx.conf。所以这是对应的 docker-compose.yml

version: '2'

services:
        myProxy:
                image: nginx
                ports: 
                        - "80:80"
                volumes:
                        - /home/me/myProxy/nginx.conf:/etc/nginx/nginx.conf

        site-a:
                image: nginx
                volumes:
                        - //home/me/site-a:/usr/share/nginx/html

        site-b:
                image: nginx
                volumes:
                        - /home/me/site-b:/usr/share/nginx/html

我还将看看 traefik,因为据我了解(以及更多功能),它允许像 nginx-proxy 这样的动态配置。

于 2019-04-25T21:36:08.997 回答