三、开始nginx
的安装和配置
1
、创建供
Nginx
运行使用的组和帐号:
# groupadd -r nginx
# useradd -r -g nginx -s /sbin/nologin -M nginx
2
、编译安装
rewrite
模块支持包
下载
pcre
包到本地,版本选择
7.0
以上版本就可以,此处是
7.8
版本
#tar zxvf pcre-7.8.tar.gz
#cd pcre-7.8/
#./configure
#make && make install
#cd ../
3
、编译安装
Nginx
服务
此处使用的
Nginx
版本为
0.7.64
,可以使用其他版本,此处使用
1.0.10
版本
wget http://sysoev.ru/nginx/nginx-1.0.10.tar.gz
#tar zxvf nginx-1.0.10.tar.gz
#cd nginx-1.0.10/
#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module
#make && make install
#cd ../
4
、备份默认
nginx.conf
配置文件
#cd /etc/nginx
#cp nginx.conf nginx.old
5
、配置
nginx
服务
(Master
和
Back up
的
nginx
配置文件是一样的
)
(
1
)、在
nginx Master
上进行配置,将
nginx
的默认配置文件修改为如下:
# cat /etc/nginx/nginx.conf
user nginx nginx;
worker_processes 8;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events
{ use epoll;
worker_connections 5120;
}
http
{
include mime.types;
default_type application/octet-stream;
charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream srtweb {
server 192.168.1.105:80; //
此处的
IP
是后端的
Real Server
的
IP
,
server 192.168.1.103:80; //
有几个
Real server
写几个
}
server {
listen 80;
server_name www.linuxgg.com; //
可以为
hostname
也可以为
localhost
location /{
root html;
index index.html index.htm;
proxy_pass http://srtweb;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
(
2
)、在
Nginx Backup
上进行配置,和
Nginx Master
上的配置一样,可以将
Nginx Master
上的
nginx.conf
配置文件拷贝到
Nginx Backup
的
/etc/nginx/
目录下面!
6
、启动测试
Nginx
服务
分别启动
Master
和
Backup
服务器上的
Nginx
服务,
#/usr/sbin/nginx
查看
Nginx
监听的端口,如果监听正常,并且显示如下图说明
Nginx
服务启动成功!
#netstat -tunlp
四、安装配置Keepalived
1
、安装
keepalived
(在
Nginx Master
和
Backup
上都要安装!)
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
#tar zxvf keepalived-1.1.19.tar.gz
#cd keepalived-1.1.19
#./configure --prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#mkdir /etc/keepalived
#cd /etc/keepalived/
2
、配置
nginx master
的
keepalived
配置文件
将
keepalived.conf
文件配置为如下内容即可!
# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.108 //Nginx Master
的
IP
地址
priority 100 //
此处的数值要比
Nginx Backup
中的高
advert_int 1
smtp_alert
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.1.100 //vip
地址
}
}
3
、配置
nginx backup
的
keepalived
配置文件
# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.110 //Nginx Backup
的
IP
地址
priority 80
advert_int 1
smtp_alert
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.1.100 //vip
地址
}
}
4
、启动
Nginx Master
和
Nginx Backup
服务器上的
keepalived
服务
查看虚拟
IP
是否绑定!
# /etc/rc.d/init.d/keepalived start
Starting keepalived: [ OK ]
(
1
)、首先在
Nginx Master
上查看
IP
绑定的情况!
红色方框部分显示
VIP
已经加载到
Nginx Master
服务器上了
!
(
2
)、其次查看
Nginx Backup
服务器上
IP
情况!
#ip addr