一台主机上的 Docker nginx 多个应用程序

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

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

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

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

最佳答案

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 可以更好、更轻松地做到这一点

关于一台主机上的 Docker nginx 多个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489482/

相关文章:

java - 在一个 docker-component 文件中依次调用多个服务

docker : can't mount some directory from host

python - Flask Nginx uWSGI 504 网关超时错误

json - nginx 不缓存 json,而是缓存 .jpg 文件

Django gunicorn nginx (111 : Connection refused) while connecting to upstream

mysql - 如何从mysql workbench连接到远程主机上的mysql docker容器?

docker - 在 docker 中附加卷后找不到模块

docker - Docker-是否可以使用weave在同一主机中连接容器

docker - 容器运行后立即退出

linux - 无法在装载/etc/passwd 和/etc/shadow 的docker 容器中添加新用户