Windows VMWare Centos Docker部署Nginx并配置对Springboot应用的访问代理
前置博文
Windows VMWare Centos环境下安装Docker并配置MySql
https://blog.csdn.net/u013224722/article/details/148928081
Windows VMWare Centos Docker部署Springboot应用
https://blog.csdn.net/u013224722/article/details/148958480
# 将已存在的容器设置为宿主机重启后自动运行
$sudo docker update --restart always dapi
一、Docker中安装使用Nginx
1、拉取Nginx镜像
sudo docker pull nginx:latest
2、创建目录用于存储拷贝出的nginx配置文件
conf目录存放配置文件,conf.d为nginx给出的的默认配置文件,html为存放html资源的目录,logs存放nginx 的日志文件,ssl目录存放ssl证书用于配置https
sudo mkdir -p /home/duel/workspace/nginx/{conf,conf.d,html,logs,ssl}
3、启动Nginx
sudo docker run --name nginx -d -p 80:80 nginx:latest
4、Nginx启动后,拷贝出配置文件
sudo docker cp nginx:/etc/nginx/nginx.conf /home/duel/workspace/nginx/conf/ #复制配置文件
sudo docker cp nginx:/etc/nginx/conf.d /home/duel/workspace/nginx
sudo docker cp nginx:/usr/share/nginx/html /home/duel/workspace/nginx #复制基本的html界面
5、停止Nginx容器并删除
# 停止运行, 移除容器
sudo docker stop nginx# 删除所有停止状态的容器
sudo docker container prune
6、创建Html静态文件
在拷贝出的存储nginx配置文件的目录中,添加一个用于测试的 html,路径和源码如下。
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><title>dweb</title>
</head>
<body>
<h1>Hi!</h1>
<p>This is dweb index.html</p>
<p>The file is located on VMware Centos Os "/home/duel/workspace/dweb"</p>
</body>
</html>
7、新建Nginx配置文件
在conf.d目录下,新增了两个文件,分别为对静态文件解析的 dweb.conf 以及对前置文章中所示创建的Springboot应用访问相关配置。
# 访问静态文件
server {listen 38080;server_name localhost;location / {root /usr/share/nginx/html/dweb;index index.html index.htm;}location ~* \.(html|css|js|png|jpg|gif|ico|mp4|mkv|rmvb|flv|eot|svg|ttf|woff|woff2|pptx|rar|zip)$ {root /usr/share/nginx/html/dweb;autoindex on;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}
}
Nginx 38081 端口的访问自动转发至 Springboot应用端口号。
# 访问前文中记录的 Springboot应用
# 8093是发布的Springboot应用使用的端口server {listen 38081;server_name localhost;location / {proxy_pass http://192.168.23.134:8093; // 端口转发}
}
8、基于新建的配置文件,重新创建Nginx容器
sudo docker run --name nginx -p 80:80 -p 443:443 -p 38080:38080 -p 38081:38081 -v /home/duel/workspace/nginx/html:/usr/share/nginx/html -v /home/duel/workspace/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/duel/workspace/nginx/conf.d:/etc/nginx/conf.d -v /home/duel/workspace/nginx/logs:/var/log/nginx -v /home/duel/workspace/nginx/ssl:/etc/nginx/ssl -d --restart=always nginx:latest
sudo docker run --name nginx -p 80:80 -p 443:443 -p 38080:38080 -p 38081:38081
-v /home/duel/workspace/nginx/html:/usr/share/nginx/html
-v /home/duel/workspace/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /home/duel/workspace/nginx/conf.d:/etc/nginx/conf.d
-v /home/duel/workspace/nginx/logs:/var/log/nginx
-v /home/duel/workspace/nginx/ssl:/etc/nginx/ssl
-d --restart=always nginx:latest
9、VMWare Centos中测试
打开Firefox浏览器、通过设置的38080端口,访问dweb目录下放置的index.html文件,成功!
通过设置的 38081 端口,访问Springboot应用提供的数据接口,成功!
10、回到Windows宿主机中测试
回到Windows宿主机,通过Chrome浏览器,访问在 VMWare Centos Docker 中发布的静态文件以及 Springboot数据接口,均成功!
二、错误的尝试
1 、 静态文件目录未映射到容器内部
浏览器访问报错 “404 Not Found”
在使用 Docker 运行 Nginx 容器时,通常需要将宿主机上的文件和目录映射到容器内部,以便于管理配置文件、静态资源等。若将静态文件放在其他目录,如下所示,将静态文件放在了“/home/duel/workspace/dweb” 目录下,创建Nginx容器时 用的指令“-v /home/duel/workspace/nginx/html:/usr/share/nginx/html” 只把 “/home/duel/workspace/nginx/html”目录映射进了容器内容。这时,Nginx启动后,浏览器访问报错 “404 Not Found”。
server {listen 38080;server_name localhost;location / {root /home/duel/workspace/dweb; // 这个目录在容器中不会映射index index.html index.htm;}location ~* \.(html|css|js|png|jpg|gif|ico|mp4|mkv|rmvb|flv|eot|svg|ttf|woff|woff2|pptx|rar|zip)$ {root /home/duel/workspace/dweb; // 这个目录在容器中不会映射autoindex on;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}
}
解决方法:将dweb目录拷贝到 ./nginx/html 目录下,并修改 conf 中的root, ./nginx/html 目录下的文件会被映射。
server {listen 38080;server_name localhost;location / {root /usr/share/nginx/html/dweb; // 映射后的目录index index.html index.htm;}
2、端口转发使用localhost或127.0.0.1
浏览器访问报错 “502 Bad Gateway”
在docker容器里,容器的IP和宿主机的IP是不一样的,127.0.0.1
指向的是容器内部的ip。
所以无法通过127.0.0.1去进行代理,必须把localhost或127.0.0.1 换成服务器的IP。
使用:proxy_pass http://127.0.0.1:8093 报 502 Bad gateway
使用:proxy_pass http://localhost:8093 报 502 Bad gateway
需要使用主机的ip,如下:
server {listen 38081;server_name localhost;location / {proxy_pass http://192.168.23.134:8093; // 使用Centos系统IP}
}