(一)nginx除了可作后端http服务器,它还是一个高性能的反向代理服务器。在负载均衡架构中,nginx可以为我们提供非常有效且稳定的基于7层的负载均衡解决方案。
(二)实现nginx反向代理及负载均衡模块,用到了httpproxymodule 与 httpupstyream模块
1 安装:
yum install gcc pcre pcre-devel openssl openssl-devel zlib-devel gd gd-devel perl -y
176 ./configure --prefix=/usr/local/nginx/
181 make &&make install
[root@server1 nginx]# du -sh
9.1M .
[root@server1 nginx]# pwd
/usr/local/nginx
2 改配置文件:
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
如下:
17 http {
18 upstream backend{
19 server 172.25.79.2:80;
20 server 172.25.79.3:80
}##!
22
23 include mime.types;
40 server {
41 listen 80;
42 server_name www.westos.com;
43
44 #charset koi8-r;
45
46 #access_log logs/host.access.log main;
47
48 location / {
49 proxy_pass http://backend; ##!
50 }
}
}
改完重启;
[root@server1 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
[root@server1 conf]# nginx -s reload
测试:
[root@niub day03]# curl 172.25.79.1
www.westos.com server3
[root@niub day03]# curl 172.25.79.1
www.westos.com server2
[root@niub day03]# curl 172.25.79.1
www.westos.com server3
[root@niub day03]# curl 172.25.79.1
www.westos.com server2
轮询,负载均衡,此时nginx是一个反向代理服务器,实现了负载均衡功能