Nginx反向代理和负载均衡

反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。

Nginx 除了可以在企业提供高性能的web服务之外,另外还可以将 nginx 本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能

ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理 7层代理

ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后端服务器分组 负载均衡

ngx_stream_proxy_module:#将客户端的请求以tcp协议转发至指定服务器处理 4层代理

ngx_http_fastcgi_module:#将客户端对php的请求以fastcgi协议转发至指定服务器助理

ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理

单台反向代理

代理服务器:172.16.88.8

真实服务器:172.16.88.9

客户机:172.16.88.7

客户端去访问代理服务器,跳到真实服务器

真实服务器:

root@node2 \~# yum install httpd -y

root@node2 \~# systemctl start httpd //此处服务起不来记得检查防火墙、nginx是否关闭

root@node2 \~# cd /var/www/html/

root@node2 html# echo "7-2 7-2 7-2" > index.html

root@node2 html# ls

index.html

客户机验证:

root@node3 \~# curl 172.16.88.9

7-2 7-2 7-2

代理服务器:

root@node1 conf.d# vim pc.conf

server {

listen 80;

server_name www.pc.com;

root /data/html;

location / {

proxy_pass http://192.168.204.20; //因为是7层必须把协议写上,写7-2真实服务器的ip

}

}

root@node1 conf.d# nginx -s reload

客户机验证:

root@node3 \~# curl 172.16.88.8

7-2 7-2 7-2

动静分离

7-1 代理服务器:172.16.88.6

7-2 静态服务器:172.16.88.7

7-3 动态服务器:172.16.88.8

7-4 客户端:172.16.88.9

`7-1 代理服务器:`

bash 复制代码
​
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
server_name  www.pc.com;
root  /data/html;
   location /api {                       //动态资源
   proxy_pass http://172.16.88.7;
   }
   location ~* \.(jpg|png|bmp|gif)$ {    //静态资源
   proxy_pass http://172.16.88.8;
   }
}

[root@node1 ~]#  nginx -t
[root@node1 ~]#  nginx -s reload

​

7-2 静态服务器:

bash 复制代码
[root@node2 ~]#  systemctl stop firewalld
[root@node2 ~]#  setenforce 0
[root@node2 ~]#  systemctl stop nginx
[root@node2 ~]#  systemctl start httpd
[root@node2 ~]#  cd /var/www/html
[root@node2 html]#  rz       //准备图片

[root@node2 html]#  ls
a.jpg

7-3 动态服务器:

bash 复制代码
[root@node3 ~]#  systemctl stop firewalld
[root@node3 ~]#  setenforce 0
[root@node3 ~]#  systemctl stop nginx
[root@node3 ~]#  systemctl start httpd
[root@node3 ~]#  cd /var/www/html/
[root@node3 html]#  ls
[root@node3 html]#  echo "7-3 dongtaifuwuqi" > index.html    //准备页面
[root@node3 html]#  ls
index.html

`7-4 客户端验证:

bash 复制代码
[root@localhost ~]#  curl 172.16.88.7/api        //动态
7-3 dongtaifuwuqi
[root@localhost ~]#  curl 172.16.88.8/a.jpg   //静态

负载均衡

nginx 代理服务器的 调度算法:

  • 轮询:一人一次
  • 加权轮询:根据权重 分配次数
  • hash算法
    • ip hash :根据ip地址来决定客户端访问的服务器
    • url hash :根据客户端访问的url来决定访问的服务器
    • cookie hash :根据cookie的值来决定访问的服务器
    • 一致性 hash
  • 最小连接算法
  • fair算法:根据响应时间来进行分配

轮询:默认一人一次

7-1 :172.16.88.7

7-2 :172.16.88.8

7-3 :172.16.88.9

7-2:

bash 复制代码
[root@node2 ~]#  yum install httpd -y
[root@node2 ~]#  systemctl start httpd
[root@node2 ~]#  cd /var/www/html/
[root@node2 html]#  ls
[root@node2 html]#  echo  222222  > index.html   //生成页面

7-3:

bash 复制代码
[root@node3 ~]#  yum install httpd -y
[root@node3 ~]#  systemctl start httpd
[root@node3 ~]#  cd /var/www/html/
[root@node3 html]#  ls
[root@node3 html]#  echo  333333  > index.html   //生成页面

7-1:

bash 复制代码
​[root@node1 ~]#  vim /apps/nginx/conf/nginx.conf //编辑主配置文件
http {
    include       mime.types;
    upstream  web {           //自定义一组服务器,配置在http块内
    server 172.16.88.8;
    server 172.16.88.9;
    } 
    [root@node1 ~]#  nginx -s reload

[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf  //编辑子配置文件
server  {
listen 80;
server_name  www.pc.com;
root  /data/html;
  location / {
  proxy_pass http://web/;
  }
}
[root@node1 ~]#  nginx -s reload

​

客户端访问172.16.88.7进行验证

相关推荐
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特3 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
张洛闻Eren3 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
布裘3 天前
【银河麒麟】V4桌面图标消失,右击鼠标没反应排查
运维·银河麒麟·桌面环境
懂软件的胡子个哥3 天前
微信机器人为什么需要“回复候选”而不是所有 AI 内容直接发送
运维·微信·自动化·wechatapi·个人微信号二次开发