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/;
}
}
**其中web1和web2 - 容器名称
码头工人-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
PS:你的问题是关于 NGINX-reverse-proxy。但是使用 TRAEFIK https://traefik.io可以更好、更轻松地做到这一点