nginx负载均衡配置实例详解

来源:互联网 发布:软件下载大全 编辑:程序博客网 时间:2024/05/23 13:27

测试域名:a.com

A服务器IP:192.168.5.149(主)

B服务器IP:192.169.5.27

C服务器IP:192.168.5.126

A服务器作为主服务器,域名直接解析到A服务器上,由A服务器负载均衡到B服务器与C服务器上。

A服务器nginx.conf设置

打开nginx.conf,文件位置在nginx安装目录的conf下,

在http段加入

updstream a.com {

server 192.168.5.126:80;

server 192.168.5.27:80;

}

server{

listen 80;

server_name a.com

location / {

proxy_pass http://a.com;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

保存重启nginx

B、C服务器nginx.conf设置

server {

listen 80;

server_name a.com

index index.html;

root /data0/htdocs/www;

}

nginx负载均衡的4中方案配置

1.轮询

根据nginx配置文件中的顺序,依次把客户端的web请求分发到不同的后端服务器

http{

upstream sampleapp {

server <<dns entry or ip address(optional with port)>>;

server <<another dns entry or ip address(optional with port)>>;

}

}

2.最少连接

web请求会被转发到连接数最少的服务器上

http{

upstream sampleapp {

least_conn;

server <<dns entry or ip address(optional with port)>>;

server <<another dns entry or ip address(optional with port)>>;

}

}

3.ip地址哈希

前述的两种负载均衡方案中,同一客户端连续的web请求可能会被分发到不同的后端服务器进行吹,因此如果涉及到会话session,那么会话会比较复杂。常见的是基于数据库的会话持久化。这时,可以使用基于IP地址哈希的负载均衡方案,这样,同一客户端连续的web请求都会被分发到同一服务器进行处理。

http{

upstream sampleapp {

ip_hash;

server <<dns entry or ip address(optional with port)>>;

server <<another dns entry or ip address(optional with port)>>;

}

}

4.基于权重

可以配置nginx把请求更多的分发到高配置的后端服务器上

http{

upstream sampleapp {

server <<dns entry or ip address(optional with port>> weight = 2;

server <<another dns entry or ip address(optional with port)>>;

}

}

0 0
原创粉丝点击