nginx反向代理及负载均衡的实现

目录

1.nginx反向代理

2.nginx负载均衡

3.nginx反向代理及负载均衡实现

nginx反向代理

4台主机都需要的操作:

两台服务器操作:

两台主机服务器进行测试;

nginx负载均衡配置

4.nginx配置其他参数

多虚拟机访问

后端服务器日志中需要记录客户端真实ip

nginx设置不同的url访问不同页面,可以实现动静分离

在代理主机上设置:

在web服务器上:

测试:

5.nginx的upstream其他参数

backup:

down:

1.nginx反向代理

原理:客户端向反向代理服务器发送请求,反向代理服务器将请求发送给相应服务器,然后服务器收到并将响应返回给反向代理服务器,由反向代理服务器将响应返回给客户端。

优点:1.可以有效防止对服务器的恶意攻击。2.减少服务器的压力。3.提高访问速度。

2.nginx负载均衡

负载均衡:防止一台服务器宕机之后,业务出现阻断。

优点:减少服务器压力,解决某时刻高并发。

3.nginx反向代理及负载均衡实现

nginx反向代理

配置准备4台主机,并在5台主机,4台上都安装nginx,另外一台客户端

4台主机都需要的操作:

复制代码
检查防火墙与selinux
1.安装epel扩展源
yum install -y epel-release

2.安装nginx
yum install -y nginx

两台服务器操作:

复制代码
配置虚拟主机
1.vim /etc/nginx/conf.d/vhost.conf
server {
	listen 80;
	server_name bbs.yunjisuan.com;
	location / {
	root /usr/share/nginx/html/bbs;
	index index.html index.htm;

}
access_log /usr/share/nginx/html/www/logs/access_www.log main;    这两个日志目录需要自己创建
}
server {
        listen 80;
        server_name www.yunjisuan.com;
        location / {
        root /usr/share/nginx/html/www;
        index index.html index.htm;

}
access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}

2.配置网页页面
mkdir -pv /usr/share/nginx/html/{bbs,www}
切到不同目录配置页面即可
echo "this is $HOSTNAME bbs" > index.html

3.重启nginx服务

两台主机服务器进行测试;

复制代码
curl -H host:bbs.yunjisuan.com 192.168.27.12
curl -H host:bbs.yunjisuan.com 192.168.27.122
curl -H host:bbs.yunjisuan.com 192.168.27.123
curl -H host:www.yunjisuan.com 192.168.27.123
curl -H host:www.yunjisuan.com 192.168.27.122

nginx负载均衡配置

负载均衡需要在两台不是服务器的主机上操作

负载均衡的关键段是upstream(定义转发池)porxy_pass (指定转发)

复制代码
两台主机:
1.编写额外配置文件
[root@lb01 ~]# more /etc/nginx/conf.d/vhosts.conf 
upstream www_server_pools {
	server 192.168.27.122:80 weight=2;    定义转发找这两个服务器
	server 192.168.27.123:80 weight=1;
}
server {
	listen 80;
	server_name www.yunjisuan.com;
	location / {
	proxy_pass http://www_server_pools;    当访问网址时,转发到这个转发池
}
}
server {
        listen 80;
        server_name bbs.yunjisuan.com;
        location / {
        proxy_pass http://www_server_pools;

}
}
2.重新启动
3.在客户端配置hosts解析文件
vim /etc/hosts
代理主机    www.yunjisuan.com bbs.yunjisuan.com
4.客户端进行测试
for ((i=1;i<=10;i++));do curl http://bbs.yunjisuan.com; done

4.nginx配置其他参数

多虚拟机访问

当配置好负载均衡后,访问bbs可能出现的页面还是www,是因为没有找到对应的请求头部信息。

配置:

复制代码
1.在代理主机上配置
[root@lb01 ~]# more /etc/nginx/conf.d/vhosts.conf 
upstream www_server_pools {
	server 192.168.27.122:80 weight=2;
	server 192.168.27.123:80 weight=1;
}
server {
	listen 80;
	server_name www.yunjisuan.com;
	location / {
	proxy_pass http://www_server_pools;
	proxy_set_header Host $host;    定义这一行
	
}
}
server {
        listen 80;
        server_name bbs.yunjisuan.com;
        location / {
        proxy_pass http://www_server_pools;
	proxy_set_header Host $host;
	

}
}

后端服务器日志中需要记录客户端真实ip

默认不会再服务器中记录客户端真实ip,只会记录代理主机,所以开启相应参数,记录客户端真实ip。

复制代码
在代理主机进行操作
[root@lb01 ~]# more /etc/nginx/conf.d/vhosts.conf 
upstream www_server_pools {
	server 192.168.27.122:80 weight=2;
	server 192.168.27.123:80 weight=1;
}
server {
	listen 80;
	server_name www.yunjisuan.com;
	location / {
	proxy_pass http://www_server_pools;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
        listen 80;
        server_name bbs.yunjisuan.com;
        location / {
        proxy_pass http://www_server_pools;
	    proxy_set_header Host $host;
	    proxy_set_header X-Forwarded-For $remote_addr;

}
}

重启并测试

nginx设置不同的url访问不同页面,可以实现动静分离

在代理主机上设置:

复制代码
vim /etc/nginx/conf.d/vhosts.conf
upstream static_pools {                    定义不同的转发池
        server 192.168.27.122:80;
}
upstream active_pools {
        server 192.168.27.123:80;
}
server {
        listen 80;
        server_name www.yunjisuan.com;
        location /static/ {
       
        proxy_pass http://static_pools;
        proxy_set_header Host $host;
}
        location /active/ {
         proxy_pass http://active_pools;
         proxy_set_header Host $host;
        }

}

重启

在web服务器上:

复制代码
两台服务器都可以操作
1.将之前的虚拟主机停到改一下后缀名称
2.配置不同页面
cd /usr/share/nginx/html/
mkdir static
cd static/
echo "this is static" > index.html
cd ..
mkdir active
echo "this is active" > index.html

重启

测试:

复制代码
curl http://www.yunjisuan.com/active/
curl http://www.yunjisuan.com/static/    一定要带最后面的/

5.nginx的upstream其他参数

backup:

当所有服务器宕机之后,会去寻找配置了backup的页面

复制代码
upstream www_server_pools {
	server 192.168.27.122:80 weight=2;
	server 192.168.27.123:80 weight=1;
    server 192.168.27.120:80 backup;
}

down:

配置之后,会将会话保持:

复制代码
upstream www_server_pools {
    ip_hash;
	server 192.168.27.122:80 weight=2;
	server 192.168.27.123:80 weight=1;
  
}

注意:backup和ip_hash不可以同时使用。

相关推荐
蹊黎3 小时前
Linux 进程概念(四):命令行参数与环境变量
linux·运维·服务器
AR-26710-6 小时前
docker Day3——镜像,容器,日志与元数据
运维·docker·容器
分布式存储与RustFS12 小时前
备份作业卡在许可证上:用 Veeam 13 把 Windows 整机备份落进 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
飞奔的瓜牛13 小时前
Linux 数据盘分区、格式化与自动挂载完整实践
linux·运维·服务器
Csvn14 小时前
Nginx HTTPS 与安全加固:会话复用、OCSP、mTLS 与限流取舍
后端·nginx
进击的荆棘15 小时前
Linux网络——Socket编程TCP
linux·运维·服务器·网络·tcp/ip
大今野15 小时前
2026 专转本计算机|Linux CentOS7 软件包管理:yum 仓库配置与常用命令考点全解(附例题)
linux·运维·服务器·专转本
无奈笑天下16 小时前
Wine适配Windows程序常见故障标准处理流程
linux·运维·服务器·windows
沫璃染墨16 小时前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组
运维·服务器·网络·网络协议·tcp/ip·ip
无足鸟ICT17 小时前
【RHCA+】hdparm命令(查看硬盘)
linux·运维·服务器