Docker中配置Nginx多域名配置多個應用

注意容器中是一個被隔離的空間,可以理解爲一個獨立的服務器,所以在做反向代理的時候,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端口給外部訪問

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章