【centos6.5】
【目录】
一、简单的负载均衡
二、负载均衡2
【以下由centos6。5操作】
一、简单的负载均衡
实验环境:
LB(负载均衡) 192.168.168.133
nginx 192.168.168.131
nginx2 192.168.168.136
1.1、安装nginx
LB(负载均衡)主机 【拉nginx包进本地】
执行以下脚本
#!/bin/bash
rpm -e httpd --nodeps
umount /dev/sr0
mkdir /media/cdrom
mount /dev/sr0 /media/cdrom
cd /etc/yum.re*
mkdir a/
mv C* a/
cp a/*M* ./
sed -i '20 s/0/1/' C*
yum -y clean all
yum makecache
service iptables stop
yum -y install pcre-devel zlib-devel gcc-c++
useradd -M -s /sbin/nologin nginx
tar xf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
cd
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx
vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; 【去掉#号】
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 123 { 【添加123,123名随便写】【这里是定义web服务池,包含了131、136的俩个web节点】
server 192.168.168.131:80 weight=1;
server 192.168.168.136:80 weight=1;
}
server { 【这里是定义代理的负载均衡域名虚拟主机】
listen 80;
server_name www.kgc.cn;
charset utf-8;
access_log logs/kgc.access.log main;
location / { 【去掉#号】
proxy_pass http://123; 【访问www.kgc.cn时,会把请求发送给123里面的节点】
index index.html index.htm;
}
保存退出
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.168.131
192.168.168.136
保存退出
1.2、编辑第二台
nginx 192.168.168.131
【拉nginx包进来本地】
#!/bin/bash
rpm -e httpd --nodeps
umount /dev/sr0
mkdir /media/cdrom
mount /dev/sr0 /media/cdrom
cd /etc/yum.re*
mkdir a/
mv C* a/
cp a/*M* ./
sed -i '20 s/0/1/' C*
yum -y clean all
yum makecache
service iptables stop
yum -y install pcre-devel zlib-devel gcc-c++
useradd -M -s /sbin/nologin nginx
tar xf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
cd
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx
makdir /kgc
echo "<h1>www.123.cn</h1>" > /kgc/index.html
vim /usr/local/nginx/conf/nginx.conf
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server_name www.123.cn;
charset utf-8;
root /kgc;
1.3、第三台
nginx2 192.168.168.136
【拉nginx包进来】
#!/bin/bash
rpm -e httpd --nodeps
umount /dev/sr0
mkdir /media/cdrom
mount /dev/sr0 /media/cdrom
cd /etc/yum.re*
mkdir a/
mv C* a/
cp a/*M* ./
sed -i '20 s/0/1/' C*
yum -y clean all
yum makecache
service iptables stop
yum -y install pcre-devel zlib-devel gcc-c++
useradd -M -s /sbin/nologin nginx
tar xf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
cd
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx
makdir /kgc
echo "<h1>www.kgc1.cn</h1>" > /kgc/index.html
vim /usr/local/nginx/conf/nginx.conf
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server_name www.kgc1.cn;
charset utf-8;
root /kgc;
保存退出
测试
[root@localhost ~]# curl 192.168.168.133
<h1>www.123.cn</h1>
[root@localhost ~]# curl 192.168.168.133
<h1>www.kgc1.cn</h1>
二、负载均衡2
LB(负载均衡) 192.168.168.133
nginx 192.168.168.131
nginx2 192.168.168.136
接着上面的实验
nginx 192.168.168.131
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.cn;
charset utf-8;
access_log logs/www.access.log main;
location / {
root /web/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.kgc.cn;
charset utf-8;
access_log logs/bbs.access.log main;
location / {
root /web/bbs;
index index.php index.html index.htm ;
}
[root@www ~]# for n in bbs www ; do mkdir -p /web/$n ; echo "$n.knc.cn" > /web/$n/index.htnl ; done
[root@www ~]# yum -y install tree
[root@www ~]# tree /web
/web
├── bbs
│ └── index.htnl
└── www
└── index.htnl
2 directories, 2 files
[root@www ~]# cat /web/bbs/index.htnl
bbs.knc.cn
[root@www ~]# cat /web/www/index.htnl
www.knc.cn
[root@nginx ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.168.131 www.kgc.cn bbs.kgc.cn