Docker nginx在一台主机上运行多个应用程序

Docker nginx在一台主机上运行多个应用程序,docker,nginx,docker-compose,jwilder-nginx-proxy,Docker,Nginx,Docker Compose,Jwilder Nginx Proxy,我对如何在同一台主机上使用多个独立的webapp管理反向代理(nginx)感到困惑。我知道我可以为每个应用程序使用和配置虚拟主机,但我无法让nginx作为服务在每个应用程序docker-compose.yml中可见 我想这样做,因为我想清楚地定义在生产中运行应用程序所需的所有服务,并在开发中轻松复制应用程序 换句话说:我有两个需要在同一台主机上运行的Web应用程序,我想在这两个应用程序中将nginx定义为docker-compose.yml中的服务依赖项,但与这两个应用程序共享该服务,因为只有一

我对如何在同一台主机上使用多个独立的webapp管理反向代理(nginx)感到困惑。我知道我可以为每个应用程序使用和配置虚拟主机,但我无法让nginx作为服务在每个应用程序docker-compose.yml中可见

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


换句话说:我有两个需要在同一台主机上运行的Web应用程序,我想在这两个应用程序中将nginx定义为docker-compose.yml中的服务依赖项,但与这两个应用程序共享该服务,因为只有一个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"]
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/;
    }
}
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
nginx.conf:

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"]
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/;
    }
}
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
**其中web1web2-容器名称

docker compose.yml:

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"]
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/;
    }
}
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
如何运行:

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"]
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/;
    }
}
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时


附言:你的问题是关于NGINX反向代理的。但是使用TRAEFIK做这件事更好更容易,您还应该能够在同一个容器中打开两个端口

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;
}
以便:

  • 本地主机上的首次应用程序访问:8080
  • 本地主机上的第二个应用程序访问:8081

  • nginx
    是一种要求还是一种好选择?无论哪种方式,我都会研究类似于
    traefik
    的东西,因此您有两个项目,其中有两个docker-compose.yml文件。你只需要一个nginx就可以同时为他们服务?两个项目是否在同一端口上运行?@ShawnC。这是一项要求,因为两个项目都需要它production@sharif9876它们不在同一个本地端口上运行,但nginx必须在端口80上侦听并将其映射到本地应用程序端口。我们可以得到2个docker compose文件的示例吗?现在我发现我的问题不是很清楚。web1和web2是两个完全独立的服务,它们之间没有任何关系。-如果web1和web2是两个完全独立的服务:1)创建可连接的网络。2) web1-连接到这个网络,web2连接到这个网络。但是nginx是分离的容器,没有包含在任何docker compose文件中。再看看这个例子,这似乎对我不起作用,我最终出现了502坏网关nginx错误。我做了一些搜索,发现它有以下几行:http{sendfile on;上游docker nginx{server nginx:80;}上游docker apache{server apache:80;}……这些是必要的吗?如果我想使用https怎么办?