Linux7层负载均衡-Nginx
特点
1. 功能强大,性能卓越,运行稳定。
2. 配置简单灵活。
3. 能够自动剔除工作不正常的后端服务器。
4. 上传文件使用异步模式。client—nginx—web1 web2 web3 lvs同步请求 DR client -->dr—web1
5. 支持多种分配策略,可以分配权重,分配方式灵活。
优势
nginx复制用户请求,在后端服务器出现问题时。nginx会再复制一份请求发给另一台后端服务器。
lvs则在这种情况,只能用户重新发请求
缺点
流量会经过nginx,nginx成为瓶颈
拓扑
7层负载均衡
通过location对不同的请求进行进行负载均衡,因为通过http协议,所以称作7层
location / {
}
location ~ .htmlKaTeX parse error: Can't use function '\.' in math mode at position 31: …. } location ~ \̲.̲php{
}
location ~ .(jpg|png|css|js)${
}
实验准备:
环境
client:client
nginx:nginx
apache:web1
apache:web2
1.关闭防火墙和selinux(略)
2.域名解析
3.同步时间(实验环境略)
计划任务执行如下命令
ntpdate 时间服务器地址
4.准备web1页面
5.配置nginx
yum install -y epel-release
yum install -y nginx
vim /etc/nginx/ngxin.conf
upstream html {
server web1:80;
server web2:80;
}
server {
location / {
proxy_pass http://html;
}
}
6.测试负载均衡
访问nginx服务器。能交替的看到web1和web2
7.动静分离
1 增加一个upstream php server web3
2.增加一个location ~ .php {}
3.示例
upstream html {
server web1:80;
server web2:80;
}
upstream php {
server web3:80;
server web4:80;
}
server {
location / {
proxy_pass http://html;
}
location ~ \.php$ {
proxy_pass http://php;
}
}
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
http {
upstream html {
server web1:80;
server web2:80;
}
upstream php {
server php1:80;
server php2:80;
}
server {
location / {
proxy_pass http://html;
}
location ~ \.php$ {
proxy_pass http://php;
}
}
}
4.客户端测试:
elinks –dump http:// nginx
elinks –dump http:// nginx/index.html
elinks –dump http:// nginx/index.php