Nginx反向代理与负载均衡应用实践
实现Nginx负载均衡的组件主要有两个,如下表:
准备4台VM虚拟机(有物理服务器更佳),两台做负载均衡,两台做RS,如下表:
HOSTNAME |
IP |
说明 |
lb01 |
192.168.0.221 |
Nginx主负载均衡器 |
lb02 |
192.168.0.222 |
Nginx副负载均衡器 |
web01 |
192.168.0.223 |
Web01服务器 |
web02 |
192.168.0.224 |
Web02服务器 |
安装Nginx软件(四台都装)
[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel
[root@localhost ~]# rpm -qa openssl openssl-devel pcre pcre-devel
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost nginx-1.10.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.10.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module && make && make install
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
配置用于测试的Web服务
将在两台NginxWeb服务器的节点上操作:配置并查看Web服务器的配置结果。
[root@localhost nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr-$remote_user[$time_local]"$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
}
- #提示:
- 这里故意将www虚拟主机放在下面,便于用后面的参数配置测试效果
配置完成后检查语法,并启动Nginx服务
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
[root@localhost nginx]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4100/nginx
然后填充测试文件数据,如下:
[root@localhost nginx]# mkdir /usr/local/nginx/html/{www,bbs}
[root@localhost nginx]# echo "`hostname -I `www" >> /usr/local/nginx/html/www/index.html
[root@localhost nginx]# cat /usr/local/nginx/html/www/index.html
192.168.0.223 www
[root@localhost nginx]# echo "`hostname -I `bbs" >> /usr/local/nginx/html/bbs/index.html
[root@localhost nginx]# cat /usr/local/nginx/html/bbs/index.html
192.168.0.223 bbs
#提示:以上操作命令,在Web01上和Web02上是一样的
配置解析Web01的IP和主机名后,用curl测试一下
[root@localhost nginx]# tail -2 /etc/hosts
192.168.0.223 www.yunjisuan.com
192.168.0.223 bbs.yunjisuan.com
[root@localhost nginx]# curl www.yunjisuan.com
192.168.0.223 www
[root@localhost nginx]# curl bbs.yunjisuan.com
192.168.0.223 bbs
配置解析Web02的IP和主机名后,用curl测试一下
[root@localhost nginx]# vim /etc/hosts
[root@localhost nginx]# tail -2 /etc/hosts
192.168.0.224 www.yunjisuan.com
192.168.0.224 bbs.yunjisuan.com
[root@localhost nginx]# curl www.yunjisuan.com
192.168.0.224 www
[root@localhost nginx]# curl bbs.yunjisuan.com
192.168.0.224 bbs
提示:
(1)不同Web测试节点,返回的结果是不同的,这是为了方便测试演示!
(2)通过上面配置就实现了两台Web服务器基于域名的虚拟主机配置。
实现一个简单的负载均衡
在nginx lb01服务器节点操作
[root@lb01 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools { #这里定义Web服务器池,包含了223,224两个Web节点
server 192.168.0.223:80 weight=1;
server 192.168.0.224:80 weight=1;
}
server { #这里定义代理的负载均衡域名虚拟主机
listen 80;
server_name www.yunjisuan.com;
location / {
proxy_pass http://www_server_pools; #访问www.yunjisuan.com,请求发送给www_server_pools里面的节点
}
}
}
现在检查语法并启动。命令如下:
[root@lb01 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@lb01 nginx]# /usr/local/nginx/sbin/nginx
[root@lb01 nginx]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4105/nginx
然后,检查负载均衡测试结果。Linux作为客户端的测试结果如下:
[root@lb01 nginx]# hostname -I
192.168.0.221
[root@lb01 nginx]# tail -1 /etc/hosts
192.168.0.221 www.yunjisuan.com #这里是lb1负载均衡器IP
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
从上面的测试结果可以看出来。两个Web节点按照1:1的比例被访问。
下面宕掉任意一个Web节点,看看测试结果如何,测试如下:
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
可以看到,网站业务不受影响,访问请求都定位到了正常的节点上。
现在,宕掉所有Web节点,此时,访问测试结果如下:
root@lb01 nginx]# curl www.yunjisuan.com
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
[root@lb01 nginx]# curl www.yunjisuan.com
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
[root@lb01 nginx]# curl www.yunjisuan.com
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
可以看到,Nginx代理下面没有节点了,因此,Nginx向用户报告了502错误。如果同时开启所有的Web服务又会怎样?测试结果如下:
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.223 bbs
[root@lb01 nginx]# curl www.yunjisuan.com
192.168.0.224 bbs
结果是Nginx又把请求一比一分配到了Nginx后面的节点上。
Nginx负载均衡核心组件介绍
Nginx upstream模块
upstream模块介绍
Nginx的负载均衡功能依赖于ngx_http_upsteam_module模块,所支持的代理方式包括proxy_pass,fastcgi_pass,memcached_pass等,新版Nginx软件支持的方式有所增加。本文主要讲解proxy_pass代理方式。
ngx_http_upstream_module模块允许Nginx定义一组或多组节点服务器组,使用时可以通过proxy_pass代理方式把网站的请求发送到事先定义好的对应Upstream组的名字上,具体写法为“proxy_pass http:// www_server_pools”,其中www_server_pools就是一个Upstream节点服务器组名字。ngx_http_upstream_module模块官方地址为:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
upstream模块语法
范例1:基本的upstream配置案例
upstream www_server_pools {
# upstream是关键字必须有,后面的www_server_pools为一个Upstream集群组的名字,可以自己起名,调用时就用这个名字
server 192.168.0.223:80 weight=5;
server 192.168.0.224:80 weight=10;
server 192.168.0.225:80 weight=15;
# server关键字是固定的,后面可以接域名(门户会用)或IP。如果不指定端口,默认是80端口。weight代表权重,数值越大被分配的请求越多,结尾有分号,别忘了。
}
范例2:较完整的upstream配置案例
upstream blog_server_pool {
server 192.168.0.223; #这行标签和下行是等价的
server 192.168.0.224:80 weight=1 max_fails=1 fail_timeout=10s; #这行标签和上一行是等价的,此行多余的部分就是默认配置,不写也可以。
server 192.168.0.225:80 weight=1 max_fails=2 fail_timeout=20s backup;
# server最后面可以加很多参数,具体参数作用看下文的表格
}
范例3:使用域名及socket的upstream配置案例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080; #域名加端口。转发到后端的指定端口上
server unix:/tmp/backend3; #指定socket文件
#提示:server后面如果接域名,需要内网有DNS服务器或者在负载均衡器的hosts文件做域名解析。
server 192.168.0.223;
server 192.168.0.224:8080;
server backup1.example.com:8080 backup;
#备份服务器,等上面指定的服务器都不可访问的时候会启动,backup的用法和Haproxy中用法一样
server backup2.example.com:8080 backup;
}
upstream模块相关说明
upstream模块的内容应放于nginx.conf配置的http{}标签内,其默认调度节点算法是wrr(weighted round-robin,即权重轮询)。下图为upstream模块内部server标签部分参数说明
http_proxy_module模块
proxy_pass指令介绍
proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器,在实际的反向代理工作中,会通过location功能匹配指定的URI,然后把接收到的符合匹配URI的请求通过proxy_pass抛给定义好的upstream节点池。该指令官方地址1见:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
下面proxy_pass的使用案例:
(1)将匹配URI为name的请求抛给http://127.0.0.1/remote/.
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
(2)将匹配URI为some/path的请求抛给http://127.0.0.1
location /some/path/ {
proxy_pass http://127.0.0.1;
}
(3)将匹配URI为name的请求应用指定的rewrite规则,然后抛给http://127.0.0.1
location /name/ {
rewrite /name/( [^/]+ ) /username=$1 break;
proxy_pass http://127.0.0.1;
}
http proxy模块参数
Nginx的代理功能是通过http proxy模块来实现的。默认在安装Nginx时已经安装了http proxy模块,因此可直接使用http proxy模块。下面详细解释模块1中每个选项代表的含义,见下表:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)