Docker.nginx部署

一、nginx发布准备

1、下载nginx镜像

Docker.nginx部署_第1张图片

2、下载spring2.0

Docker.nginx部署_第2张图片Docker.nginx部署_第3张图片

3、下载nginx容器

命名为nginx01

Docker.nginx部署_第4张图片

 现在能访问nginx了,映射nginx镜像成功

Docker.nginx部署_第5张图片

4、 操作nginx发布前端项目

①、nginx默认下载在这个文件夹

Docker.nginx部署_第6张图片

②、进入conf.d,修改default.conf

Docker.nginx部署_第7张图片Docker.nginx部署_第8张图片

default.conf:

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

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

    location / {
        root   /usr/share/nginx/html;
        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.42.145: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;
    #}
}

③、进入nginx01容器

在/etc/nginx 配置nginx环境

修改conf.d里面的default.conf

没有安装vim,修改不了

Docker.nginx部署_第9张图片

 退出Ctrl+D

删除所有容器:

④、 将配置文件挂载到容器中去

(1)新建nginx目录:
Docker.nginx部署_第10张图片

 进入nginx目录,新建conf.d,html,log目录

Docker.nginx部署_第11张图片

(2) 进入conf.d,新建default.conf

Docker.nginx部署_第12张图片

default.conf:

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.42.145:8080;
    }
}

(3) 进入html,将vue.zip放进去

Docker.nginx部署_第13张图片

解压:

Docker.nginx部署_第14张图片

 新建nginx01容器

Docker.nginx部署_第15张图片

 直接浏览器访问

Docker.nginx部署_第16张图片

二、nginx反向代理

点击后台访问报错,

Docker.nginx部署_第17张图片

解决这一问题:

1、 新建spring01容器并启动:
Docker.nginx部署_第18张图片

  浏览器访问 

 Docker.nginx部署_第19张图片

 2、修改default.conf:

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:8081;
    }
}

3、新开服务进入容器更新数据

查看配置是否出错

 重新读取配置文件

三、nginx实现负载均衡

1、创建集群

修改default.conf:

upstream tomcatList {  
    server 172.17.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;
    }  
}

现在后台访问就成功了

Docker.nginx部署_第20张图片

 2、查看负载均衡轮回

Docker.nginx部署_第21张图片

修改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两次

Docker.nginx部署_第22张图片

Docker.nginx部署_第23张图片 

 四、容器跨网段数据交互

1,删除spring01和spring02

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

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

Docker.nginx部署_第24张图片

 3、网段地址变换

①、查看mynet

Docker.nginx部署_第25张图片

Docker.nginx部署_第26张图片

 ②、修改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

Docker.nginx部署_第27张图片

 5、浏览器访问

Docker.nginx部署_第28张图片

 

你可能感兴趣的:(nginx,docker,容器)