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

Nginx 反向代理与负载均衡核心内容总结 - 实践

Nginx 反向代理与负载均衡核心内容总结 - 实践

Nginx 反向代理与负载均衡核心内容总结

一、核心概念解析

1. 代理基础

代理是连接被代理角色与目标角色的渠道,类似专卖店作为厂家(被代理方)与用户(目标方)的中间枢纽,可分为正向代理与反向代理两种核心模式

2. 正向代理

3. 反向代理

  • 场景:Nginx 接收客户端请求后,按规则分发至后端多台业务服务器处理
  • 特点:客户端明确,但请求具体由哪台后端服务器处理未知;隐藏后端服务器信息,多用于服务器集群部署
  • 实际应用:常与正向代理结合使用 —— 正向代理客户端请求访问目标服务器,该目标服务器实为反向代理,连接多台真实业务服务器

二、Nginx 核心功能实现

1. 动静分离

  • 原理:反向代理时,静态资源直接从 Nginx 发布路径读取,无需请求后端服务器,提升响应效率。
  • 关键保障:需保证前后端程序一致性,可通过 Rsync 自动同步,或 NFS、MFS 分布式共享存储实现资源统一
  • 依赖模块:Http Proxy 模块为核心,常用proxy_pass(转发请求)和proxy_cache(缓存资源)指令;使用proxy_cache需安装时集成第三方ngx_cache_purge模块,用于清除指定 URL 缓存,安装命令为./configure --add-module=…/ngx_cache_purge-1.0 …

2. 负载均衡

三、Nginx 实战案例

1. 负载均衡配置案例

2. 动静分离配置案例

  • 环境扩展:新增 Tomcat 主机(192.168.100.40),部署测试页面(输出 “Hello World”)

  • 核心配置:定义静态资源集群(Rs1、Rs2)和 Tomcat 集群,通过不同location分发请求

    nginx

    http {upstream static {server 192.168.100.20;server 192.168.100.30;}upstream tomcat {server 192.168.100.40:8080;}server {listen 80;server_name localhost;location / {proxy_pass http://static; # 静态资源请求转发至static集群}location /test {proxy_pass http://tomcat; # 动态请求转发至Tomcat}}
    }
  • 测试效果:访问 Nginx 根路径获取静态页面,访问/test路径获取 Tomcat 动态页面,实现动静分离

具体详细示例:

三台主机都关闭防火墙和selinux,还需要配置好yum仓库

nginx主机已部署nginx服务

rs1主机上,安装httpd,然后添加一个测试网页:

[root@rs1 ~]# cd /etc/yum.repos.d/
[root@rs1 yum.repos.d]# ls
sy.repo
[root@rs1 yum.repos.d]# cat sy.repo
[aa]
name=aa1
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@rs1 yum.repos.d]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@rs1 yum.repos.d]# yum -y install httpd
[root@rs1 yum.repos.d]# echo "RS1" > /var/www/html/index.html
[root@rs1 yum.repos.d]# systemctl restart httpd
[root@rs1 yum.repos.d]# systemctl enable httpd

rs2主机上,安装httpd,然后添加一个测试网页:

[root@rs2 ~]# cd /etc/yum.repos.d/
[root@rs2 yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@rs2 yum.repos.d]# rm -rf *
[root@rs2 yum.repos.d]# vim sy.repo
[aa]
name=aa1
baseurl=file:///mnt
enabled=1
gpgcheck=0
~
[root@rs2 yum.repos.d]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@rs2 yum.repos.d]# yum -y install httpd
[root@rs2 yum.repos.d]# echo "RS2" > /var/www/html/index.html
[root@rs2 yum.repos.d]# systemctl restart httpd
[root@rs2 yum.repos.d]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

在nginx主机上,修改配置文件,设置负载均衡

在http{}中配置upstream:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {include       mime.types;default_type  application/octet-stream;upstream webserver {server 192.168.100.20;server 192.168.100.30;}location / {proxy_pass http://webserver;}

测试配置文件是否正确,然后重载nginx:

[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload

测试访问:

在这里插入图片描述

在这里插入图片描述

设置权重,如果想其中一台后端真实服务器,多承担一些访问量,可以去设置weight权重:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {include       mime.types;default_type  application/octet-stream;upstream webserver {server 192.168.100.20 weight=2;server 192.168.100.30;}

重载nginx并测试访问,此时会发现100.20主机(rs1),访问时,访问的2次后,才轮询到rs2中:

[root@nginx ~]# nginx -s reload

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

三台主机都是使用80端口,所以,在nginx配置文件中的upstream中,对应的真实后端服务器,我们并没有设置端口。如果其中某台后端服务器使用8080端口呢?我们如何进行设置,rs1为8080端口,rs2为80端口

首先修改rs1的httpd服务,侦听8080端口,并重启httpd服务:

[root@rs1 ~]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@rs1 ~]# systemctl restart httpd

在nginx中:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {include       mime.types;default_type  application/octet-stream;upstream webserver {server 192.168.100.20:8080;server 192.168.100.30;}
[root@nginx ~]# nginx -s reload

测试访问:

在这里插入图片描述

在这里插入图片描述

ip_hash配置:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {include       mime.types;default_type  application/octet-stream;upstream webserver {ip_hash;server 192.168.100.20:8080;server 192.168.100.30;}
[root@nginx ~]# nginx -s reload

在这里插入图片描述

在这里插入图片描述

ip_hash这种负载均衡模式根据个人理解就是:例如多个用户通过nginx访问到了后端的httpd集群中,这个时候因为有不同用户,所以ip也不同,ip+hash算法计算的hash值都传到了httpd,nginx就记录了这个ip和hash值,那么下次同一个ip过来还是会分配到这个httpd的

如果在集群中的某台服务器出现故障,我们想要从nginx的集群配置中移除掉,我们不可以直接的将那一行删掉,比如server 192.168.100.10:8080 weight=2;删掉,如果直接删掉会导致nginx的hash算法重新计算,那么用户的会话或者说缓存都会失效掉,所以这里如果不用这台服务器,直接比较为down即可,也就是 server 192.168.100.10:8080 down 这么做就可以了

动静分离nginx+tomcat 还是基于上面的环境添加一台tomcat

Tomcat:192.168.100.40

部署tomcat:

上传压缩包并解压:

[root@client ~]# rz -E
rz waiting to receive.
[root@client ~]# ls
anaconda-ks.cfg               Documents             Music     Templates
apache-tomcat-10.0.23.tar.gz  Downloads             Pictures  Videos
Desktop                       initial-setup-ks.cfg  Public
[root@client ~]# tar -zxvf apache-tomcat-10.0.23.tar.gz -C /usr/local/
[root@client ~]# cd /usr/local/
[root@client local]# ls
apache-tomcat-10.0.23  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@client local]# ln -s apache-tomcat-10.0.23/ tomcat
[root@client local]# cd tomcat/
[root@client tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@client tomcat]# cd webapps/
[root@client webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@client webapps]# cd ROOT/
[root@client ROOT]# ls
asf-logo-wide.svg  bg-middle.png  bg-upper.png  index.jsp          tomcat.css  WEB-INF
bg-button.png      bg-nav.png     favicon.ico   RELEASE-NOTES.txt  tomcat.svg
[root@client ROOT]# ls
asf-logo-wide.svg  bg-middle.png  bg-upper.png  index.jsp          tomcat.css  WEB-INF
bg-button.png      bg-nav.png     favicon.ico   RELEASE-NOTES.txt  tomcat.svg
[root@client ROOT]# cp index.jsp index.jsp.bak
[root@client ROOT]# vim index.jsptest page<%out.println("Hello,World");%>~
[root@client ROOT]# cd ..
[root@client webapps]# cd ..
[root@client tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@client tomcat]# bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@client tomcat]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN     0      128               *:111                           *:*
LISTEN     0      128               *:6000                          *:*
LISTEN     0      5      192.168.122.1:53                            *:*
LISTEN     0      128               *:22                            *:*
LISTEN     0      128       127.0.0.1:631                           *:*
LISTEN     0      100       127.0.0.1:25                            *:*
LISTEN     0      128       127.0.0.1:6010                          *:*
LISTEN     0      128              :::111                          :::*
LISTEN     0      100              :::8080                         :::*
LISTEN     0      128              :::6000                         :::*
LISTEN     0      128              :::22                           :::*
LISTEN     0      128             ::1:631                          :::*
LISTEN     0      100             ::1:25                           :::*
LISTEN     0      128             ::1:6010                         :::*
LISTEN     0      1      ::ffff:127.0.0.1:8005                         :::*             [root@client webapps]# mkdir test
[root@client webapps]# cp ROOT/index.jsp test/

部署完成后,测试访问以下tomcat的测试网页:

在这里插入图片描述

配置nginx,设置动静分离:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {include       mime.types;default_type  application/octet-stream;upstream static {server 192.168.100.20;server 192.168.100.30;}upstream tomcat{server 192.168.100.40:8080;}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {proxy_pass http://static;}location /test {proxy_pass http://tomcat;}
[root@nginx ~]# nginx -s reload

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

http://www.sczhlp.com/news/150723/

相关文章:

  • 厦门百度网站建设深圳网站设计公司行业
  • 网站建设需招聘什么专业人建设网站时候应该注意哪些
  • php毕业设计代做网站物联网今天正式开网
  • 家教网站怎么做顺营销官方网站
  • 如何维护网站的运营信用网站建设原则
  • 网站可以建几个人百度做自己的网站
  • 宝安网站-建设深圳信科海南网站运营托管咨询
  • 北京注册网站asp源码下载网站
  • 网站使用帮助内容湖南省公共资源交易中心
  • 工作号做文案素材的网站图片如何连接到wordpress
  • asp.net 网站 代理河源市做网站
  • 信管女生做网站开发南京小视科技是干什么的
  • 营销型网站郑州北京公司摇号中签率
  • 模板建站优缺点淘宝网络营销案例分析
  • 微网站 demo制作外贸网站的公司简介
  • 好网站推荐的网站c2c模式类型
  • 柘城网站建设网站截图可以做证据吗
  • 厦门网络公司网站个人单页网站建设
  • 网站不同颜色网站如何做视频点播
  • 郑州专业做网站的公司潍坊注册公司流程和费用标准
  • 盗qq的钓鱼网站怎么做淘宝上的网站怎么做
  • 原核蛋白表达与真核蛋白表达的差异选择
  • 纯文本网站在什么网站做贸易好
  • 做网站怎么找客户联系方式wordpress外部样式
  • 清新太和做网站深圳百度推广网站建设
  • 网站流量指的是什么意思网站关键词优化软件效果
  • 泛型类型参数
  • 做玩具什么 网站比较好帮别人做网站必须要开公司
  • 最专业的礼品网站案例参考百度推广获客成本大概多少
  • 顺企网我做网站公司网站设计好