注意容器中是一个被隔离的空间,可以理解为一个独立的服务器,所以在做反向代理的时候,nginx配置不能使用localhost,可以使用link方式去访问其他容器
nginxa:
container_name: nginxa
image: registry.cn-shenzhen.aliyuncs.com/beni/nginx:latest
volumes:
- C:/data/nginxa/conf.d:/etc/nginx/conf.d
- C:/data/nginxa/www:/usr/share/nginx/html
nginxb:
container_name: nginxb
image: registry.cn-shenzhen.aliyuncs.com/beni/nginx:latest
volumes:
- C:/data/nginxb/conf.d:/etc/nginx/conf.d
- C:/data/nginxb/www:/usr/share/nginx/html
nginxc:
container_name: nginxc
image: registry.cn-shenzhen.aliyuncs.com/beni/nginx:latest
ports:
- 80:80
volumes:
- C:/data/nginxc/conf.d:/etc/nginx/conf.d
links:
- nginxa:nginxa
- nginxb:nginxb
配置
server {
listen 80;
server_name a.pytask.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://nginxa:80/;
}
}
server {
listen 80;
server_name b.pytask.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://nginxb:80/;
}
}
a.pytask.com访问应用a
b.pytask.com访问应用b
其中a和b两个容器可以不做端口映射,因为c是用过link方式访问a和b
c做的是反向代理,开放80端口给外部访问