当前位置: 首页 > news >正文

Windows VMWare Centos Docker部署Nginx并配置对Springboot应用的访问代理

 前置博文

Windows VMWare Centos环境下安装Docker并配置MySqlhttps://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}
}

http://www.dtcms.com/a/262969.html

相关文章:

  • k8s一键部署tongweb7容器版脚本(by why+lqw)
  • 车辆工程中的压力传感技术:MEMS与薄膜传感器的实战应用
  • 22.安卓逆向2-frida hook技术-app使用非http协议抓不到包解决方式
  • Linux 安装使用教程
  • Pytest自动化测试框架入门?
  • Kafka 核心机制面试题--自问自答
  • 在Flutter中生成App Bundle并上架Google Play
  • 「Java EE开发指南」如何用MyEclipse创建一个WEB项目?(三)
  • 鸿蒙NEXT-鸿蒙三层架构搭建,嵌入HMRouter,实现便捷跳转,新手攻略。(2/3)
  • Flutter视频压缩插件video_compressffmpeg_kit_flutter_new
  • Memcached 安装使用教程
  • Flutter插件ios_pod
  • httpd-devel 与服务无关
  • Java历史:从橡树到火星探索,从微软法律战到Spring、Gradle
  • [6-02-01].第05节:配置文件 - 读取配置文件的内容
  • 一、(基础)构建一个简单的 LangChain 应用
  • 对称非对称加密,https和http,https通讯原理,Charles抓包原理
  • macos 使用 vllm 启动模型
  • WIFI 低功耗保活知识系列---三.WiFi AP如何广播自己的缓存区信息
  • OpenCV CUDA模块设备层----计算向量的平方根函数sqrt
  • 基于Spring Boot的绿园社区团购系统的设计与实现
  • Python 安装使用教程
  • Spring Boot 启动性能优化实战指南
  • 基于 SpringBoot+Vue.js+ElementUI 的 Cosplay 论坛设计与实现7000字论文
  • 【硬核数学】2.7 理论与现实的鸿沟:深度学习的数值稳定性挑战《从零构建机器学习、深度学习到LLM的数学认知》
  • 【Spring】——事务、整合、注解
  • 后台管理系统模板Art Design Pro
  • js代码03
  • Karmada 多集群服务发现
  • Apache Doris Profile 深度解析:从获取到分析,解锁查询性能优化密码