首页 > 代码库 > LAMP、LNMP实战之八搭建nginxLB

LAMP、LNMP实战之八搭建nginxLB

LAMP、LNMP实战之七搭建nginxLB
说明:服务器192.168.2.138
192.168.2.137
192.168.2.40
nginx版本nginx-1.6.2.tar.gz
1、安装nginx
yum install pcre pcre-devel openssl openssl-devel -y //安装pcre、openssl
yum install gcc -y //安装gcc
cd /home/www/tools //进入tools目录
将nginx压缩包放在此目录
tar zxf nginx-1.6.2.tar.gz //解压nginx
cd nginx-1.6.2 //进入nginx目录
useradd nginx -s /sbin/nologin -M //创建nginx用户
./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2 --with-http_stub_status_module --with-http_ssl_module //编译
make && make install //编译
ln -s /application/nginx1.6.2/ /application/nginx //创建软连接
/application/nginx/sbin/nginx //启动nginx
打开IE浏览器输入服务器网址如果网页显示“Welcome to nginx!”说明安装成功

2、配置负载均衡
cd /application/nginx/conf //进入配置目录
egrep -v "^$|#" nginx.conf.default >nginx.conf //筛选配置文件内容
vi nginx.conf //编辑配置文件,改为以下内容

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend {
server 192.168.2.137:80 max_fails=3 fail_timeout=30s;
server 192.168.2.40:80 max_fails=3 fail_timeout=30s;
}

server {
listen 80;
server_name www.zhaojunjian.com;
index index.html index.htm;
location / {
proxy_pass http://backend;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
:wq //保存退出
/application/nginx/sbin/nginx -s reload //重启服务
for n in `seq 100`;do curl 192.168.2.138;sleep 2;done //访问测试,每次内容不一样为正常

3、测试
for n in `seq 100`;do curl 192.168.2.138;sleep 2;done //访问测试,每次内容不一样为正常(138)
killall nginx //关闭137nginx服务
查看138访问是否切换到40上
killall httpd //关闭40apache服务
查看138访问是否异常
/application/nginx/sbin/nginx //开启137nginx服务
查看138访问是否正常访问137
/application/apache/bin/apachectl start //开启40apache服务
查看138访问是否正常轮询访问40和137

LAMP、LNMP实战之八搭建nginxLB