【问题标题】:Docker nginx multiple apps on one host一台主机上的 Docker nginx 多个应用程序
【发布时间】:2018-09-04 11:32:46
【问题描述】:

我对如何在同一主机上管理具有多个独立 web 应用程序的反向代理 (nginx) 感到困惑。我知道我可以使用https://github.com/jwilder/nginx-proxy 并为每个应用程序配置 VIRTUAL_HOST,但是我将无法在每个应用程序 docker-compose.yml 中让 nginx 作为服务可见。

我想这样做是因为我想明确定义在生产中运行应用程序所需的所有服务,并在开发中轻松复制它。

换句话说:我有两个需要在同一主机上运行的 web 应用程序,我想在两个应用程序的 docker-compose.yml 中将 nginx 定义为服务依赖项,但与两者共享该服务,因为只有一个 nginx 可以转发端口 80。

【问题讨论】:

  • nginx 是必需品还是值得拥有?无论哪种方式,我都会研究类似traefik
  • 所以你有 2 个项目,有 2 个 docker-compose.yml 文件。而您只需要 1 个 nginx 来为他们提供服务吗?两个项目是否在同一个端口上运行?
  • @ShawnC。这是一个要求,因为两个项目都需要它进行生产
  • @sharif9876 它们不在同一个本地端口上运行,但 nginx 必须侦听端口 80 并将其映射到本地应用程序端口
  • 我们可以获取 2 个 docker-compose 文件的示例吗?

标签: docker nginx docker-compose jwilder-nginx-proxy


【解决方案1】:

Dockerfile:

FROM ubuntu:14.04
MAINTAINER Test (test@example.com)
# install nginx
RUN apt-get update -y
RUN apt-get install -y python-software-properties
RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:nginx/stable
RUN apt-get update -y
RUN apt-get install -y nginx
# deamon mode off
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx
# volume
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/var/log/nginx"]
# expose ports
EXPOSE 80 443
# add nginx conf
ADD nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /etc/nginx
CMD ["nginx"]

nginx.conf:

server {
    listen          80;
    server_name     test1.com www.test1.com;
    location / {
        proxy_pass  http://web1:81/;
    }
}
server {
    listen          80;
    server_name     test2.com www.test2.com;
    location / {
        proxy_pass  http://web1:82/;
    }
}

** 其中 web1web2 - 容器名称

docker-compose.yml:

version: "2"
services:
    web1:
        image: your_image
        container_name: web1
        ports:
            - 81:80
    web2:
        image: your_image
        container_name: web2
        ports:
            - 82:80
    nginx:
        build: .
        container_name: nginx
        ports:
            - 80:80
            - 443:443
        links:
            - web1
            - web2

如何运行:

docker-compose up -d

当您调用 test1.com - nginx 将您的请求转发到容器 web1:81, 当 test2.com - 到容器 web2:82

P.S.:您的问题是关于 NGINX-reverse-proxy。但是使用 TRAEFIK https://traefik.io 可以更好、更轻松地做到这一点

【讨论】:

  • 现在我发现我的问题不是那么清楚。 web1 和 web2 是两个完全独立的服务,彼此没有任何关系。
  • - 如果 web1 和 web2 是两个完全独立的服务:1) 创建可附加网络。 2) web1 - 连接到这个网络,web2 连接到这个网络。
  • 但是 nginx 是分离的容器,不包含在任何 docker compose 文件中
  • 这似乎对我不起作用,我最终得到了 502 bad gateway nginx 错误。我做了一些搜索,发现这个thepolyglotdeveloper.com/2017/03/… 有以下几行 http { sendfile on;上游 docker-nginx { 服务器 nginx:80; } 上游 docker-apache { 服务器 apache:80; } ....这些有必要吗?
【解决方案2】:

您还应该能够在同一个容器中打开两个端口

services:
web:
    image: your_image
    container_name: web
    ports:
        - 8080:80
        - 8081:81

并且在 nginx 站点启用(或 conf.d)中为第二个应用添加新的配置文件,它将监听 81 端口。

第一个应用程序

server {
listen 80;
server_name localhost;
root /app/first;
}

第二个应用

server {
listen 81;
server_name localhost;
root /app/second;
}

这样:

  • 首次应用访问 localhost:8080
  • 在 localhost:8081 上的第二次应用访问

【讨论】:

  • 如果我想使用 https 怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多