Nginx 反向代理、负载均衡

Nginx 反向代理

​ 反向代理,其实对于客户端是无感的,因为客户端不需要任何配置就可以访问到web页面,Client 只需要将 请求发送给反向代理服务器,再由反向代理服务器去选择目标服务器获取资源数据,返回给客户端,此时反向代理服务区和目标服务区对外就是一个服务器。暴露的是代理服务器地址,隐藏了真实的服务器地址。

在这里插入图片描述

为什么要是使用 代理服务器

  • 提升访问速度
    • 由于目标主机返回的数据会存放在代理服务器的硬盘中,因此下次客户端在访问相同的网站会快一点
  • 充当防火墙的作用
    • 隐藏了web服务器的地址,避免不发份子对 web造成一定影响

Nginx 反向代理模块

upstream { } 模块 位于 http{ } 模块之中。

upstream 表示 负载均衡服务器的地址池

  • 格式
    • name wei 集群的名字
http {
	upstream name {
		server ip:port;
		server ip:port;
	}
	
	server {
	…………
	}
}

Nginx 反向代理 轮询方式:

  • 静态: 负载均衡服务器根据规则进行分配( 普通轮询,加权轮询,ip_hash,url_name)

  • 动态: 负载均衡服务器根据后端节点状态进行分配( fair )

1.普通轮询

  • 每个请求按照时间顺序统一分配到不同的服务器
upstream		name	{

server ip:port

server ip:port

}
  1. 加权轮询

  • ​ 指定轮询几率,weight权重和访问率成正比
    • weight 权重,轮询的几率
upstream name {

server ip:port	weight=10;

server ip:port	weight=15;

}

3. ip_hash方式

  • 每个请求按照访问ip的hash结果分配;每个客户端固定访问一个服务器
upstream name {

ip_hash;

server ip:prot;

server ip:port;

}

4. fair(第三方)

  • 按照服务器响应时间分配,响应迅速的优先分配
upstream name {

server ip:port;

server ip:port;

fair;

}

5. url_hash(第三方)

  • 同样需要第三方模块进行支持
    • 根据url的hash结果分配请求,将固定的url分配给同一个服务器

6. upstream 选项

选项含义
weight权重
down表示服务器暂时不参与负载均衡
backup表示服务器作为备用服务器
max_fails=1尝试连接后端服务器失败的次数
fail_timeout=time创建连接够短服务器的间隔时间 (秒)

7. 实现 Nginx反向代理

​ 添加了 upstream 模块后,在 server { location { } } 中添加

location {
	proxy_pass http://name;					# root\index 选项不要
	}

8. Nginx负载均衡与反向代理

  • 代理服务
    • 反向代理服务器位于用户与目标服务器之间,但是对于用户来说,反向代理服务器就是目标服务器,也就是说用户直接访问代理服务器就可以获取到目标服务器中的资源以及网页内容。

通过反向代理实现负载均衡需要:

1)ngx_http_proxy_module		代理模块

2)ngx_http_upstream_module	负载均衡模块		
  • 不需要单独添加,在安装NGINX时,自动添加

实验环境:

服务器地址
反向代理服务器1.1.1.101/8
nginx_web11.1.1.102/8
nginx_web21.1.1.103/8

1) 安装三台nginx

​ 点击这里查看Nginx 安装 《《

  • 将三台服务器的80端口开放
[root@localhost ~]# firewall-cmd --add-service=http --permanent

success

[root@localhost ~]# firewall-cmd --reload

success
  • 查看nginx 安装了哪些模块
[root@localhost objs]# pwd 

/usr/src/nginx-1.11.1/objs

[root@localhost objs]# vim ngx_modules.c 

2) 配置负载均衡服务器

  • 添加 upstream 模块
[root@localhost ~]# cd /usr/local/nginx/conf/

[root@localhost conf]# vim nginx.conf

	http {#gzip  on;

​        upstream www.server.pools {			# 开启负载均衡; weight:权重

​                server 1.1.1.102:80 weight=1;

​                server 1.1.1.103:80 weight=1;
       }

  server {

………………

​      }								# 结束以上模块重写或者修改也可以

​    server {

​        listen  80;

​        server_name www.yrz.com;

​        location / {

​                proxy_pass http://www.server.pools;}}

​     server {

​        listen  80;

​        server_name www.cjk.com;

​        

​        location / {

​                proxy_pass http://www_server_pools;}

3) 检查配置文件

[root@localhost ~]# 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

4) 编写nginx_web1 网页 与 nginx_web2网页

[root@localhost ~]# echo "server nginx_web1 1.1.1.102" > /usr/local/nginx/html/index.html 

[root@localhost nginx-1.11.1]# echo "server nginx_web2 1.1.1.10" > /usr/local/nginx/html/index.html 

5) 重新加载nginx配置文件

[root@localhost ~]# nginx -s reload

6) 配置DNS解析文件(因 没有配置DNS服务器)

  • 没有配置 DNS ,偷个懒
[root@localhost ~]# echo "1.1.1.101 www.yrz.com

\> 1.1.1.101 www.cjk.com" > /etc/hosts

7) 访问 1.1.1.101 、 www.yrz.com 、 www.cjk.com

  • 分别查看有什么区别
[root@localhost ~]# curl 1.1.1.101

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

…………………………
[root@localhost ~]# curl www.yrz.com

server nginx_web1 1.1.1.102
[root@localhost ~]# curl www.yrz.com

server nginx_web2 1.1.1.10
[root@localhost ~]# curl www.cjk.com

server nginx_web1 1.1.1.102
[root@localhost ~]# curl www.cjk.com

server nginx_web2 1.1.1.10

nx!

…………………………


[root@localhost ~]# curl www.yrz.com

server nginx_web1 1.1.1.102


[root@localhost ~]# curl www.yrz.com

server nginx_web2 1.1.1.10


[root@localhost ~]# curl www.cjk.com

server nginx_web1 1.1.1.102


[root@localhost ~]# curl www.cjk.com

server nginx_web2 1.1.1.10


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值