一、nginx发布准备
1、下载nginx镜像
2、下载spring2.0
3、下载nginx容器
命名为nginx01
现在能访问nginx了,映射nginx镜像成功
4、 操作nginx发布前端项目
①、nginx默认下载在这个文件夹
②、进入conf.d,修改default.conf
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,修改不了
退出Ctrl+D
删除所有容器:
④、 将配置文件挂载到容器中去
(1)新建nginx目录:
进入nginx目录,新建conf.d,html,log目录
(2) 进入conf.d,新建default.conf
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放进去
解压:
新建nginx01容器
直接浏览器访问
二、nginx反向代理
点击后台访问报错,
解决这一问题:
1、 新建spring01容器并启动:
浏览器访问
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;
}
}
现在后台访问就成功了
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
5、浏览器访问