nginx不仅是一个强大的web服务器,也是一个反向代理服务器。具有动静分离,轮询,权重,ip哈希,url哈希等负载均衡方式。

环境

前端服务器: server 192.168.11.100(nginx) 后端服务器: apache 192.168.11.101(apache) apache 192.168.11.102(apache)

轮询(默认)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[root@100 nginx]# tree conf/
conf/
├── extra
│   └── test.conf
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf

1 directory, 16 files

nginx.conf http模块填写include extra/*.conf;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@100 nginx]# vi /usr/local/nginx/conf/extra/test.conf 
upstream web-server
{
        server 192.168.11.101:80;
        server 192.168.11.102:80;
}
server
{
        listen 80;
        server_name test.com;
        location / {
                proxy_pass http://web-server;
        }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@100 sbin]# curl test.com
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>

weight

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@100 sbin]# vi /usr/local/nginx/conf/extra/test.conf 
upstream web-server
{
        server 192.168.11.101:80 weight=2;
        server 192.168.11.102:80 weight=8;
}
server
{
        listen 80;
        server_name test.com;
        location / {
                proxy_pass http://web-server;
        }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>
[root@100 sbin]# curl test.com
<html><body><h1>It works!102 dynamic</h1></body></html>

ip_hash

ip_hash是根据ip因子来分配后端服务器,需要nginx为最前端服务器。同时如果后端存在其他方式的负载均衡,会导致用户请求无法精准定位到之前的访问的同一台服务器,可借助upstream_hash第三方模块,一般还需使用url_hash。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[root@100 sbin]# vi /usr/local/nginx/conf/extra/test.conf 
upstream web-server
{
        server 192.168.11.101:80 weight=2;
        server 192.168.11.102:80 weight=8;
		ip_hash;
}
server
{
        listen 80;
        server_name test.com;
        location / {
                proxy_pass http://web-server;
        }
}

根据目录实现动静分离

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[root@100 sbin]# vi /usr/local/nginx/conf/extra/test.conf 
upstream web-static
{
        server 192.168.11.101:80;
}       
upstream web-dynamic
{
        server 192.168.11.102:80;
}
server
{
        listen 80;
        server_name test.com;
        location /static/ {
                proxy_pass http://web-static/;
        }
        location /dynamic/ {
                proxy_pass http://web-dynamic/;
        }
}
1
2
3
4
[root@100 sbin]# curl test.com/static/
<html><body><h1>It works!101 static</h1></body></html>
[root@100 sbin]# curl test.com/dynamic/
<html><body><h1>It works!102 dynamic</h1></body></html>