博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lvs,nginx反向代理,虚拟主机
阅读量:4351 次
发布时间:2019-06-07

本文共 9626 字,大约阅读时间需要 32 分钟。

LVS NAT

拓扑

client       |       |       LVS       |       |-------------------|        |        |RS1      RS2      RS3

地址规划如下

机器名称 ip配置 ip配置 备注信息
LVS 192.168.2.23 192.168.11.11 2块网卡
RS1 192.168.11.12
RS2 192.168.11.13

检查内核支持支持lvs

[root@lvs ~]# grep -i vs /boot/config-3.10.0-229.el7.x86_64 有IP_VS段说明支持

安装ipvsadm工具

yum install -y ipvsadm

ipvsadm语法

ipvsadm :

  管理集群服务
    添加:-A -t|u|f service-address [-s scheduler]
      -t:TCP协议
      -u:UDP协议
      -f:FWM,防火墙标记
    修改:-E
    删除:-D -t|u|f service-address
    
    # ipvsadm -A -t 192.168.2.23:80 -s rr
  管理集群服务中的RS
    添加:-a -t|u|f service-address -r server-address [-g|i|m] [-w weight]
    -t|u|f service-address:事先定义好的某集群服务
    -r server-address:某RS的地址,在NAT模型中,可使用IP:PORT实现映射
    [-g|i|m]:LVS类型
      -g:DR模型
      -i:TUN模型
      -m:NAT模型
    [-w weight]:定义服务器权重
    修改:-e
    删除:-d -t|u|f service-address -r server-address
    
    # ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.11 -m
    # ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.13 -m
    
    查看:-L|l
      -n:数字格式显示主机地址和端口
      --stats:统计数据
      --rate:速率
      --timeout:显示tcp、tcpfin和udp的会话超时时长
      -c:显示当前的ipvs连接状况
      
删除所有集群服务  

        -C:清空ipvs规则

    保存规则  
      -S  
      # ipvsadm -S > /ath/to/somfefile
    载入此前的规则:  
      -R
      # ipvsadm -R < /path/from/somefile
      

RS服务器上安装nginx

创建LVS

[root@lvs ~]# ipvsadm -A -t 192.168.2.23:80 -s rr[root@lvs ~]# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.12 -m[root@lvs ~]# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.13 -m

打开ip转发机制

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

sysctl -p 使配置文件生效

测试结果

root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2

      

LVS DR

LVS各模式的路由方式及区别:

NAT模式:客户端-->分发路由器-->Real Server-->分发路由器-->客户机

DR模式:客户端-->分发路由器--Real Server-->客户机
TUN模式:客户机-->分发路由器--Real Server-->客户机

实验拓扑

client       |       |      Router       |       |------------------------------------|                 |                   |RS1               LVS                RS3Rip:192.168.2.72  Rip:192.168.2.23  Rip:192.168.2.104Vip:192.168.2.200  Vip:192.168.2.200  Vip:192.168.2.200

配置LVS

ip addr add 192.168.2.200/32 dev eno16777736:1ipvsadm -A -t 192.168.2.200:80 -s rripvsadm -a -t 192.168.2.200:80 -r 192.168.2.72 -gipvsadm -a -t 192.168.2.200:80 -r 192.168.2.104:80 -g

arp_ignore与arp_announce参数

arp响应限制arp_ignore

arp_ignore参数的作用是控制系统在收到外部的ARP请求, 是否要回返ARP响应。

  • 0 - 默认值:响应任意网卡上接收到的对本机IP地址的ARP请求(包括回环地址),而不管该目的IP是否在接收网卡上
  • 1 - 只响应目的IP地址为接收网卡上的本地地址的ARP请求
  • 2 - 只响应目的IP地址为接收网卡上的本地地址的ARP请求,并且ARP请求的源IP必须和接收网卡同网段
  • 3 - 如果ARP请求数据包所请求的IP地址对应的本地地址作用域(scope)为主机(host),则不回应ARP响应数据包,如果作用域为全局(global)或链路(link),则回应ARP响应数据包。
  • 4 - 7 保留未使用
  • 8 - 不回应所有的ARP查询

    arp响应限制arp_announce

    arp_announce的任务是控制系统在对外发送ARP请求时,如何选择ARP请求数据包的源IP地址。(比如系统准备通过网卡发送一个数据包a,这时数据包a的源IP和目的IP一般是知道的,而根据目的IP查询路由表,发送网卡也是确定的,故源MAC地址也是知道的,这时就差确定目的MAC地址了。而想要获取目的IP对应的目的MAC地址,就需要发送ARP请求。ARP请求的目的IP自然就是想要获取其MAC地址的IP,而ARP请求的源IP是什么呢?可能第一反应会以为肯定是数据包a的源IP地址,但是这个也不是一般的,ARP请求的源IP是可以选择的,控制这个地址如何选择就是arp_announce的作用)
  • 0 - 默认允许使用任何网卡上的IP地址作为ARP请求的源IP,通常就是使用数据包a的源IP
  • 1 - 尽量避免使用不属于该发送网卡子网的本地地址作为发送ARP请求的源地址
  • 2 - IP数据包的源IP地址,选择该发送网卡上最合适的本地地址作为ARP请求的源IP地址

    配置Real Server

ip addr add 192.168.2.200/32 dev lo:1echo 1 > /proc/sys/net/ipv4/conf/eno16777736/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/eno16777736/arp_announce

测试结果

root@node2:~# curl http://192.168.2.200/index.htmlrs1root@node2:~# curl http://192.168.2.200/index.htmlrs2root@node2:~# curl http://192.168.2.200/index.htmlrs1root@node2:~# curl http://192.168.2.200/index.htmlrs2root@node2:~# curl http://192.168.2.200/index.htmlrs1root@node2:~# curl http://192.168.2.200/index.htmlrs2

Nginx反向代理

什么是代理服务器

代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务器接受客户请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的磁盘中,再发送给客户机。

为什么要使用代理服务器

  • 提高访问速度
    由于目标主机返回的数据会放在代理服务器的磁盘中,因此下一次客户再访问相同的站点数据时,会直接从代理服务器的磁盘中读取,起到了缓存的作用,尤其对于热门站点能明显提高请求速度
  • 防火墙作用
    由于所有的客户机请求都必须通过代理服务器访问远程站点,因此可在代理服务器上设限,过滤某些不安全信息
  • 通过代理服务器访问不能访问的目标站点

    互联网上有施工开发的代理服务器,客户机在访问受限时,可通过不受限的代理服务器访问目标站点

    反向代理VS正向代理

  • 正向代理
    架设在客户机与目标主机之间,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。
  • 反向代理
    架设在服务器前端,通过缓冲经常被请求的而面来缓解服务器的工作量,将客户请求转发给内部网络上的目标服务器;并将服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器
  • 反向代理有哪些主要应用

    现在许多大型web网站都用到反向代理。除了可以防止网网对内网服务器的恶意攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。

    配置Nginx为反向代理服务器

    新建配置文件/etc/nginx/conf.d/proxy.conf

upstream websrvs {        server 192.168.2.72:80;        server 192.168.2.104:80;}server {        listen 80 default_server;        server_name  _;        location / {                proxy_pass http://websrvs;                index index.html ;        }}

测试结果

root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.htmlrs1root@node2:~# curl http://192.168.2.23/index.htmlrs2root@node2:~# curl http://192.168.2.23/index.html

Nginx虚拟主机

基于IP的虚拟主机

如果一台服务器有多个IP,可以使用基于IP的虚拟主机配置,将不同的服务绑定在不同的IP上。

在服务器上配置3个IP地址

[root@lvs conf.d]# ip addr add 192.168.2.151/24 dev eno16777736:2[root@lvs conf.d]# ip addr add 192.168.2.152/24 dev eno16777736:3[root@lvs conf.d]# ip addr add 192.168.2.154/24 dev eno16777736:4

建立虚拟主机存放网页的根目录,将创建首页文件index.html

[root@lvs conf.d]# mkdir -pv /data/www/15{1..3}mkdir: 已创建目录 "/data"mkdir: 已创建目录 "/data/www"mkdir: 已创建目录 "/data/www/151"mkdir: 已创建目录 "/data/www/152"mkdir: 已创建目录 "/data/www/153"[root@lvs conf.d]# echo server151 > /data/www/151/index.html[root@lvs conf.d]# echo server152 > /data/www/152/index.html[root@lvs conf.d]# echo server153 > /data/www/153/index.html

编辑虚拟主机配置文件

/etc/nginx/conf.d/test.conf

server {        listen 192.168.2.151:80;        server_name www.test.com;        location / {                root /data/www/151;                index index.html;        }}server {        listen 192.168.2.152:80;        server_name www.test.com;        location / {                root /data/www/152;                index index.html;        }}server {        listen 192.168.2.153:80;        server_name www.test.com;        location / {                root /data/www/153;                index index.html;        }}

测试结果

root@node2:~# curl http://192.168.2.153/index.htmlserver153root@node2:~# curl http://192.168.2.152/index.htmlserver152root@node2:~# curl http://192.168.2.151/index.htmlserver151

基于端口的虚拟主机

如果一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置

创建虚拟主机存放网页的目录,并创建首页文件index.html

[root@lvs conf.d]# mkdir /data/www/{7..9}081[root@lvs conf.d]# echo "port 7081" > /data/www/7081/index.html[root@lvs conf.d]# echo "port 8081" > /data/www/8081/index.html[root@lvs conf.d]# echo "port 9081" > /data/www/9081/index.html

编辑虚拟主机配置文件

/etc/nginx/conf.d/port.conf

server {        listen 192.168.2.155:7081;        server_name www.port.com;        location / {                root /data/www/7081;                index index.html;        }}server {        listen 192.168.2.155:8081;        server_name www.port.com;        location / {                root /data/www/8081;                index index.html;        }}server {        listen 192.168.2.155:9081;        server_name www.port.com;        location / {                root /data/www/9081;                index index.html;        }}

测试结果:

root@node2:~# curl http://192.168.2.155:7081/index.htmlport 7081root@node2:~# curl http://192.168.2.155:8081/index.htmlport 8081root@node2:~# curl http://192.168.2.155:9081/index.htmlport 9081

基于域名的虚拟主机

使用基于域名的虚拟主机是比较流行的方式,可以在同一个IP上配置多个域名,并且都通过80端口访问

建立虚拟主机存放网页的目录,并创建首面文件index.html

[root@lvs conf.d]# mkdir /data/www/www.oa.com[root@lvs conf.d]# mkdir /data/www/www.bbs.com[root@lvs conf.d]# mkdir /data/www/www.test.com[root@lvs conf.d]# echo www.oa.com > /data/www/www.oa.com/index.html[root@lvs conf.d]# echo www.bbs.com > /data/www/www.bbs.com/index.html[root@lvs conf.d]# echo www.test.com > /data/www/www.test.com/index.html

建立虚拟主机配置文件

/etc/nginx/conf.d/vhost.conf

server {        listen 192.168.2.155:80;        server_name www.oa.com;        location / {                root /data/www/www.oa.com;                index index.html;        }}server {        listen 192.168.2.155:80;        server_name www.bbs.com;        location / {                root /data/www/www.bbs.com;                index index.html;        }}server {        listen 192.168.2.155:80;        server_name www.test.com;        location / {                root /data/www/www.test.com;                index index.html;        }}

测试结果

root@node2:~# curl http://www.oa.com/index.htmlwww.oa.comroot@node2:~# curl http://www.bbs.com/index.htmlwww.bbs.comroot@node2:~# curl http://www.test.com/index.htmlwww.test.com

参考文档:

https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_021_lvsnat.html
https://www.cnblogs.com/knowledgesea/p/6407018.html#undefined
https://www.cnblogs.com/lipengxiang2009/p/7451050.html
https://blog.csdn.net/Daybreak1209/article/details/51549031
https://blog.csdn.net/liupeifeng3514/article/details/79006998
https://blog.csdn.net/liupeifeng3514/article/details/79007035
https://blog.csdn.net/liupeifeng3514/article/details/79007051

转载于:https://www.cnblogs.com/feng-land/p/10424170.html

你可能感兴趣的文章
有道语料库爬虫
查看>>
VS2019 实用设置
查看>>
for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度
查看>>
CFileDialog
查看>>
[转载]EXTJS学习
查看>>
SQL Server2012完全备份、差异备份、事务日志备份和还原操作
查看>>
Flash动画播放
查看>>
springmvc+mybatis+dubbo+zookeeper 分布式架构
查看>>
HDUOJ-----Computer Transformation
查看>>
HDUOJ-----2838Cow Sorting(组合树状数组)
查看>>
自定义控件之---抽屉式弹窗控件.
查看>>
一款纯css3实现的机器人看书动画效果
查看>>
加班与效率
查看>>
轻量级Modal模态框插件cta.js
查看>>
MyEclipse下SpringBoot+JSP整合过程及踩坑
查看>>
重定向和管道
查看>>
实验五
查看>>
STL学习笔记(第二章 C++及其标准程序库简介)
查看>>
Operator_countByValue
查看>>
Java 日期往后推迟n天
查看>>