Linux之nginx负载均衡
Nginx#
负载均衡分类#
- LVS四层负载
- Nginx haproxy七层负载
Nginx基于应用进行负载#
- 静态文件:apache nginx
- 动态文件:aoache tomcat
- 图片:squid
Nginx负载均衡的方式#
- 轮询:数据依次分配给后端服务节点 如果服务器宕机自动剔除
- 权重:根据不同的优先级分配不同的请求次数的
- ip_hash:基于IP进行hash结过分配 保证每个客户端访问一个固定的服务器 可以解决session问题
- fair:根据后端服务器响应时间 响应短的优先分配
- url_hash:根据访问url进行hash 访问相同的url定向到后端特定的服务器
Nginx搭建#
网络拓扑#

安装#
依赖包#
[root@Nginx ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
[root@Nginx ~]# yum -y install gcc gcc-c++ autoconf automake
配置编译#
[root@Nginx ~]# tar -xvf nginx-1.14.1.tar.gz -C /usr/local/src/
[root@Nginx ~]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
[root@Nginx ~]# make -j 4 && make install
参数讲解
配置nginx用户#
[root@Nginx nginx-1.14.1]# useradd -M -s /sbin/nologin nginx
配置Nginx环境变量#
[root@Nginx nginx]# vim /etc/profile
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
[root@Nginx nginx]# nginx -v

启动nginx#
[root@Nginx nginx]# nginx
[root@Nginx nginx]# netstat -aunpt | grep 80
修改配置文件#
[root@Nginx conf]# cd /usr/local/nginx/conf/
[root@Nginx conf]# cp nginx.conf nginx.conf.bak # 备份配置文件
[root@Nginx conf]# vim nginx.conf
user nobody --> user nginx nginx;
location / {
root html;
index index.html index.htm;
# 43行添加如下内容
if ($request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#定义负载均衡服务器组名称
upstream htmlservers {
# 后端服务器真实节点
server 10.1.1.2:80;
server 10.1.1.4:80;
}
upstream phpservers{
server 10.1.1.2:80;
server 10.1.1.4:80;
}
upstream picservers {
server 10.1.1.2:80;
server 10.1.1.4:80;
}
# 检测nginx
[root@Nginx conf]# 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
# 重载nginx
[root@Nginx conf]# nginx -s reload
后端节点配置#
Node1节点配置#
# 关闭安全策略
[root@Node1]# systemctl stop firewalld.service && setenforce 0 && iptables -F
# 安装httpd
root@Node1 ~]# yum install httpd php -y
# 生成静态测试文件:
[root@Node1 ~]# echo Node1 test page > /var/www/html/index.html

# 测试PHP
[root@Node1 ~]# vim /var/www/html/test.php
Node1 test pag
<?php
phpinfo();
?>
# 测图片
[root@Node1 ~]# mv /var/www/html/pic.jpg

Node2节点配置#
# 关闭安全策略
[root@Node2]# systemctl stop firewalld.service && setenforce 0 && iptables -F
# 安装httpd
root@Node2 ~]# yum install httpd php -y
# 生成静态测试文件:
[root@Node2 ~]# echo Node2 test page > /var/www/html/index.html

[root@Node2 ~]# vim /var/www/html/test.php
Node2 test page
<?php
phpinfo();
?>
# 测图片
[root@Node2 ~]# mv /var/www/html/pic.jpg

ab压测#
[root@client ~]# ab -n 1000 -c 1000 http://10.1.1.1/index.html

nginx负载方法#
权重#
用于服务器性能不均匀的情况 性能强的服务器分配的数据多
upstream backserver {
server 10.1.1.2 weight=1;
server 10.1.1.4 weight=2;
}
ip_hash#
按照请求的IP地址经过hash之后分配到一个固定的服务器 保证每个IP访问一个固定的服务器 用来解决session问题
upstream backserver {
ip_hash;
server 10.1.1.2:80;
server 10.1.1.4:80;
}
fair#
按照服务器响应时间 响应时间短被优先分配
upstream backserver {
fair;
server 10.1.1.2:80;
server 10.1.1.4:80;
}
url_hash#
按访问 url 的 hash 结果来分配请求,使每个 url 定向到同一个后端服务器,后端服务器为缓存时比较有效
upstream backserver {
hash $request_uri;
hash_method crc32;
server 10.1.1.2:80;
server 10.1.1.4:80;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!