一,部署单个

1,拉取 nginx 镜像  默认下载的是最新版本

docker pull nginx

2,创建nginx01容器并且对nginx所需文件进行目录挂载

docker run -itd \
--name nginx01 \
-v /myconf/nginx/conf.d:/etc/nginx/conf.d \
-v /myconf/nginx/html:/etc/nginx/html \
-v /myconf/nginx/log:/usr/log/nginx \
-p 8080:80 nginx

3,nginx conf.d 配置文件 

 
    server {
 
    listen  80;
    server_name  www.zking.com;
 
    location / {
        root   /etc/nginx/html/;
        index  index.html index.htm;
    }
 
    error_page   500 502 503 504  /50x.html;
    
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location ^~/api/ {
       # nginx监听当以api开头请求时转发地址,后端容器ip
        proxy_pass  http://178.17.1.4:8080/;
        
    }
 
}

1,后端镜像  后端集成了jdk的运行环境

docker build -t spring:2.0 .

2,后端容器

docker run -it --name spring01 -p 8081:8081 spring:2.0

二,部署集群

1,在新建俩个容器集成后端镜像

2,修改nginx配置文件

#服务器的集群
upstream tomcatList {  #服务器集群名字
    server 172.17.0.3:8081 weight=1;
    server 172.17.0.4:8081 weight=2;
    server 172.17.0.5:8081 weight=2;
} 
    
server {

    listen  80;
    server_name  www.lgs.com;

    location / {
        root   /etc/nginx/html/;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location ^~/api/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass  http://tomcatList;
        proxy_redirect default;
    }

}

3,更新nginx配置

检查配置是否出差
nginx -t
重新加载配置
nginx -s reload

三,不同端口解决方法  

docker network connect mynet mynginx

 OK!到这就结束了,希望能帮到你!!!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐