Docker05.nginx部署

一.发布准备

 

今天我门换了一个jar,所以干脆换了一个镜像

修改default.conf文件

#服务器的集群
upstream tomcatList {  #服务器集群名字
    server 172.18.0.5:8081 weight=1;  #服务器1   weight是权重的意思,权重越大,分配的概率越大
    server 172.18.0.3:8081 weight=2;  #服务器2   weight是权重的意思,权重越大,分配的概率越大
    server 172.18.0.4:8081 weight=2;  #服务器2   weight是权重的意思,权重越大,分配的概率越大
} 
    
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/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass  http://tomcatList;
        proxy_redirect default;
    }

}

把要用的jar包导入

 

创建镜像 spring:2.0 

docker build -t spring2.0  .

创建nginx镜像中的容器

docker run -itd --name nginx -p 8082:80 nginx

  

 进入nginx的文件目录

cd /etc/nginx

 打开  nginx.conf

 红线部分:conf.d本身是一个文件夹,这句话意思是,它会把所有conf.d文件夹下所有以conf结尾的文件全部加载进来,所以这个文件里面没有做配置

所以就进入conf.d  查看default.conf文件

 cd conf.d

server {
    listen       80;
    server_name  www.zking.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/local/tools/vue;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    location ~^/spa/ {
        proxy_pass   http://192.168.253.129:8080;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

把里面得文件信息配到容器里

 docker exec -it nginx bash

 进入 cd /etc/nginx/

 cd /etc/nginx/

可以看到有个 default.conf 文件  ,今天任务其实只要把它改成相应得就行了

 但这里有个问题,这个文件在容器内,我们的工具不能进入容器内,相应的肯定就会使用 vim命令 或者 vi命令  

可以看到我们的镜像不支持vim和vi命令,理所当然,缺哪个软件我们就下哪个软件,但是下软件很麻烦。

解决办法:

1.直接打包一个具备centos的镜像再发布 (也麻烦)

2.我们可以把文件拿到宿主机里面,然后使用目录挂载把文件挂载到容器里去

 做法:

先进入home文件夹,创建 nginx目录

cd /home

 mkdir nginx

 把我们的配置文件静态资源前端项目 挂载到容器

1.建立对应的文件夹

 mkdir {conf.d,html,log}

 放入对应的文件

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 ~^/spa/ {
        proxy_pass   http://192.168.253.131:8080;
    }

  }

 解压

 unzip vue.zip

解压之后 挂载

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

访问8081

 

但是点击后台访问的时候控制台会报错

 二.nginx反向代理

基于spring2.0镜像创建容器

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

运行之后运行(顺应不能乱)

 Ctrl p  Ctrl q

进入conf.d修改default文件

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/ {
        proxy_pass   http://192.168.253.131:8082;
    }

  }

 再次修改default文件,修改之前查看桥段

 docker inspect spring01

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/ {
        proxy_pass   http://172.17.0.3:8082;
    }

  }

 新开一个窗口,进入nginx01容器中

docker exec -it nginx01 bash

重新读取配置文件

nginx -t

nginx -s reload

再次修改default文件,修改成单个集群

#服务器的集群
upstream tomcatList {  #服务器集群名字
    server 172.17.0.3:8081 weight=1;  #服务器1   weight是权重的意思,权重越大,分配的概率越大
} 
    
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/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass  http://tomcatList;
        proxy_redirect default;
    }

  }

 2、查看负载均衡轮回

修改default.conf: weight权重

upstream tomcatList {  
    server 172.17.0.3:8081 weight=1;
    server 172.17.0.4:8081 weight=1;
} 

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/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass  http://tomcatList;
        proxy_redirect default;
    }
}

 浏览器访问四次

spring01两次,spring02两次

 

 三、容器跨网段数据交互 

1,删除spring01和spring02

2、再次新建spring01和spring02·,修改语句

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

 3、网段地址变换

①、查看mynet

 ②、修改default.conf

upstream tomcatList {  
    server 172.18.0.2:8081 weight=1;
    server 172.18.0.3:8081 weight=1;
}  

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/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass  http://tomcatList;
        proxy_redirect default;
    }
}

 会出现响应超时问题!还是会访问不到

4、双网覆盖

docker network connect mynet nginx01

 

今天内容就到此为止了

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值