Nginx+proxy实现简单的负载均衡

环境说明:
操作系统centos6.6 64位
web操纵系统是:web1=192.168.10.10(LAMP) web2=192.168.10.11(LNMP),
这里只是测试nginx实现负载均衡效果,故不安装mysql、php了,站点名称为:bbs.360blog.top
负载均衡服务器 192.168.10.21 (虚拟vip:192.168.10.20)
测试机:192.168.10.22
部署完成后的目的是:
当192.168.10.10宕机时,vip192.168.10.20能够自动漂移到192.168.10.11
当192.168.10.11宕机时,vip192.168.10.20能够自动漂移到192.168.10.10
所有软件压缩包都存放在/server/tools
所有应用程序安装目录存放在/application
具体操作步骤:


一、在192.168.10.10上面安装 apache
1)添加www用户
useradd -M -s /sbin/nologin www

2)上传httpd-2.2.22.tar.gz到/server/tools下

3)解压编译并安装
cd /server/tools
tar xf httpd-2.2.22.tar.gz
./configure --prefix=/application/apache2.2.22 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite
make && make install
echo $?
ln -s /application/apache2.2.22/ /application/apache
/application/apache/bin/apachectl -t
/application/apache/bin/apachectl start
查看80端口是否开启
lsof -i :80

4)把apache加入开机自启动
cat>>/etc/rc.local<<EOF
/application/apache/bin/apachectl
EOF

5)修改apache配置文件httpd.conf
vim /application/apache/conf/httpd.conf
在66行 User daemon 修改为 User www
在67行 Group daemon 修改为 Group www
在98行 #ServerName www.example.com:80 添加一行 ServerName 127.0.0.1:80
在145行 Options Indexes FollowSymLinks 修改为 Options -Indexes FollowSymLinks
在167行 DirectoryIndex index.html 修改为 DirectoryIndex index.html index.php
在309行下面添加
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
在378行将 #Include conf/extra/httpd-mpm.conf 修改为 Include conf/extra/httpd-mpm.conf
在396行将#Include conf/extra/httpd-vhosts.conf修改为Include conf/extra/httpd-vhosts.conf
在405行将#Include conf/extra/httpd-default.conf 修改为 Include conf/extra/httpd-default.conf
在文件末尾添加
<Directory "/data/www/bbs">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

6)修改vim /application/apache/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/data/www/bbs"
ServerName bbs.360blog.top
#ServerAlias www.dummy-host.example.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>

7)创建目录/data/www/bbs,并新建index.html,内容为bbs.360blog.top 10
mkdir -p /data/www/bbs
chown -R www.www /data/
touch /data/www/bbs/index.html
vim /data/www/bbs/index.html
bbs.360blog.top 10

8)启动apache,并访问结果
/application/apache/bin/apachectl start



二、在192.168.10.11,192.168.10.21安装nginx
1)安装pcre-devel支持nginx伪静态,安装openssl-devel库支持加密算法
yum install pcre pcre-devel openssl openssl-devel -y

2)创建软件存放目录
mkdir -p /server/tools

3)创建nginx用户并解压nginx软件、编译安装
useradd -M -s /sbin/nologin nginx
cd /server/tools
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make && make install

4)创建软连接
ln -s /application/nginx-1.6.3/ /application/nginx

5)启动nginx
/application/nginx/sbin/nginx


在192.168.10.11上面操作
1)编辑配置文件nginx.conf
vim /application/nginx/conf/nginx
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
2)创建目录extra,并创建bbs.conf文件
mkdir /application/nginx/conf/extra
touch /application/nginx/conf/extra/bbs.conf
3)创建站点bbs目录,并新建index.html,添加内容 bbs.360blog.top 11
mkdir /application/nginx/html/bbs/ -p
touch /application/nginx/html/bbs/index.html
vim /application/nginx/html/bbs/index.html
bbs.360blog.top 11
4)编辑配置文件bbs.conf
vim /application/nginx/conf/extra/bbs.conf
server {
listen 80;
root html/bbs;
server_name bbs.360blog.top;
location / {
root html/bbs;
index index.html phpinfo.php index.php index.htm;
}

5)检查语法,并平滑重启nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
6)将nginx服务,加入开机自启动
echo “/application/nginx/sbin/nginx”>>/etc/rc.local
7)访问网站

三、配置反向代理服务器来实现负载均衡
在192.168.10.21上面操作
1)添加虚拟vip:192.168.10.20
ip addr add 192.168.10.20/24 dev eth0
2)编辑配置文件nginx.conf
vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
upstream bbs_server_pools{
#ip_hash; #nginx的hash调度算法,当一个客户端访问web1时,那么下次访问还是web1
server 192.168.10.10; #nginx轮询rr调度算法
#server 192.168.10.10 weight=5; #nginx权重调度算法
server 192.168.10.11;
}
#include extra/lb_www.conf;
include extra/lb_bbs.conf;
#include extra/lb_blog.conf;
}
3)创建目录extra,创建文件lb_bbs.conf
mkdir /application/nginx/conf/extra -p
touch /application/nginx/conf/extra/lb_bbs.conf
编辑文件lb_bbs.conf
vim /application/nginx/conf/extra/lb_bbs.conf
server {
listen 192.168.10.20:80;
server_name bbs.360blog.top;
location / {
proxy_pass http://bbs_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
4)启动nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx

四、测试结果
在192.168.10.22上面测试
1)修改hosts主机
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.20 bbs.360blog.top
保存退出
2)测试机访问反向代理服务器192.168.10.21
[root@proxy1 ~]# for i in `seq 1 10`;do curl bbs.360blog.top/index.html;done
bbs.360blog.top 10
bbs.360blog.top 11
bbs.360blog.top 10
bbs.360blog.top 11
bbs.360blog.top 10
bbs.360blog.top 11
bbs.360blog.top 10
bbs.360blog.top 11
bbs.360blog.top 10
bbs.360blog.top 11
#nginx+proxy 轮询rr调度算法,平均分配
查看web1服务器日志


查看web2服务器日志

此时我们看到的结果是客户端192.168.10.22访问反向代理服务器时是通过虚拟vip192.168.10.20访问,查看web1和web2服务器日志都是监听到反向代理服务器的真实ip192.168.10.21.
那是因为当客户端请求192.168.10.20的时候,而200这个虚拟ip是作为eth0网卡的次ip,实际通讯是通过主ip192.168.10.21出去的,故web1和web2服务器中日志监听到的192.168.10.21ip


#nginx+proxy 权重调度算法
具体设置返回上面nginx反向代理服务器,设置配置文件nginx.conf
在客户端192.168.10.22查看演示结果


#nginx+proxy ip_hash调度算法

至此nginx+proxy反向代理实现负载均衡讲解完毕
课外知识:
负载均衡开源软件:
L4:tcp负载均衡 lvs
L4-L7:Haproxy
L7(http): Nginx+proxy

参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
http://nginx.org/en/docs/http/ngx_http_memcached_module.html

原文地址:https://www.cnblogs.com/yihr/p/7443482.html