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

docker(7):实战--安装nginx并实现反向代理

基本概念

反向代理:客户端向反向代理的命名空间中的内容发送普通请求,接着反向代理将推断向何处(原始服务器)转交请求,并将获得的内容返回给客户端。

负载均衡:当请求过多,单个服务器难以负荷时,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上。

  1. 拉取镜像
docker pull nginx
  1. 创建要挂载的配置文件
vim  nginx.conf

内容如下:

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}
  1. 设置反向代理和负载均衡

    假设我们有个spring服务,启动了两个实例,端口分别为10010和10086。现在想让nginx对我们两个实例进行反向代理,并能够做简单的负载均衡。

修改配置文件增加反向代理配置:

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;# 注意这里默认的配置注释掉,否则80端口会使用默认的配置# include /etc/nginx/conf.d/*.conf;# 代理负载均衡,默认是轮询upstream proxytest {server localhost:10010;server localhost:10086;}server {listen 80;server_name localhost;# 使用代理location / {# proxytest是上面我们配置的upstream的名字proxy_pass http://proxytest;}}}
  1. 启动nginx
docker run --name nginx-proxy -p 80:80 -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -d nginx

这样就实现了使用nginx代理localhost:10010和localhost:10086两个服务了。这两个属于同一个spring服务的不同端口。

http://www.lryc.cn/news/174136.html

相关文章:

  • day-61 代码随想录算法训练营(19)一刷完结撒花
  • C#中对泛型集合元素使用List.Sort()方法排序
  • 【项目】在线音乐播放器测试报告
  • [C++ 网络协议] 多线程服务器端
  • 宝塔部署node后使用pm2管理上传文件路径失效问题
  • postman-pre-request-scripts使用
  • uniapp Echart X轴Y轴文字被遮挡怎么办,或未能铺满整个容器
  • 学习路之PHP--laravel DingoApi
  • 项目篇——java文档搜索引擎
  • 5.2 磁盘CRC32完整性检测
  • 企业内部安全与风控管理图解
  • vscode基于cmake安装opencv库
  • Web 器学习笔记(基础)
  • uniapp中vue3使用uni.createSelectorQuery().in(this)报错
  • k8s-部署
  • Arduino驱动MMA7260三轴加速度传感器(惯性测量传感器篇)
  • 奇舞周刊第507期:通过 View Transition API 在状态之间添加丰富的过渡动画
  • 如何通过技术变现
  • 高效查询大量快递信息,轻松掌握技巧
  • iperf3: error - unable to connect to server: No route to host 但嵌入式Linux设备
  • OpenCV自学笔记十七:傅里叶变换
  • uniapp如何判断是哪个(微信/APP)平台
  • 网络安全——(黑客)自学
  • 一拖三快充线(USB-C转三充)的解决方案--LDR6020P
  • 2024年考研教育专业的教育综合考试大纲、样题和往年真题
  • 算法之斐波那契数列
  • 关于Pandas数据分析
  • Go 并发可视化解释 - sync.Mute
  • 十几张高清世界地图
  • Python 逢七拍手游戏