nginx+keepalived安装配置(整理中)
線上環境安裝測試說明
兩臺測試機
nginx+keepalived做后端LNMP及tomcat+jdk+mysql的負載高可用
測試環境
centos 6.2 x86_64
IP:192.168.101.77
vip:10.10.10.8
centos 6.2 x86_64
IP:192.168.101.88
vip:10.10.10.8
由于測試環境,兩臺都安裝同樣的服務
安裝過程
192.168.101.77和192.168.101.88 安裝nginx+keepalived
安裝依賴包
yum -y install cmake gcc gcc-c++ make zlib zlib-devel openssl* popt popt-devel pcre pcre-devel
安裝pcre
[root@open2 src]# tar jxvf pcre-8.20.tar.bz2 ? ? ? ?
[root@open2 src]# cd pcre-8.20 ? ? ? ? ? ? ? ? ? ? ? ? ? ?
[root@open2 pcre-8.20]# ./configure --prefix=/usr/local/app/pcre
[root@open2 pcre-8.20]# make && make install
[root@open2 pcre-8.20]# cd ..
安裝nginx
[root@open2 src]# tar zxvf nginx-1.5.6.tar.gz
[root@open2 src]# cd nginx-1.5.6
[root@open2 nginx-1.5.6]# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/src/pcre-8.20
[root@open2 nginx-1.5.6]# make
[root@open2 nginx-1.5.6]# make install
[root@open2 nginx-1.5.6]# cd ..
安裝keepalived
[root@open2 src]# tar zxvf keepalived-1.2.8.tar.gz ? ? ? ? ?
[root@open2 src]# cd keepalived-1.2.8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
[root@open2 keepalived-1.2.8]# ./configure --prefix=/usr/local/app/keepalived
[root@open2 keepalived-1.2.8]# make
[root@open2 keepalived-1.2.8]# make install
配置文件,啟動腳本
[root@open2 keepalived-1.2.8]# cp /usr/local/app/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@open2 keepalived-1.2.8]# cp /usr/local/app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@open2 keepalived-1.2.8]# mkdir /etc/keepalived
[root@open2 keepalived-1.2.8]# cp /usr/local/app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@open2 keepalived-1.2.8]# cp /usr/local/app/keepalived/sbin/keepalived /usr/sbin/
[root@open2 keepalived-1.2.8]# chkconfig --level 2345 keepalived on
[root@open2 keepalived-1.2.8]# service keepalived start
Starting keepalived: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [ ?OK ?]
[root@open-source keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
? ?link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
? ?inet 127.0.0.1/8 scope host lo
? ?inet6 ::1/128 scope host
? ? ? valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
? ?link/ether 00:0c:29:c5:78:f3 brd ff:ff:ff:ff:ff:ff
? ?inet 192.168.101.77/24 brd 192.168.101.255 scope global eth0
? ?inet 192.168.101.7/32 scope global eth0
? ?inet 10.10.10.8/32 scope global eth0 ? ? ? ? ? ? ? ? ? #查看VIP是否啟動
? ?inet6 fe80::20c:29ff:fec5:78f3/64 scope link
? ? ? valid_lft forever preferred_lft forever
192.168.101.77 keepalived配置,192.168.101.88配置一樣只是換一下IP
[root@open2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
? notification_email {
? ? ? ?leeforget@test.com
? }
? notification_email_from leeforget@test.com
? smtp_server 192.168.101.77
? smtp_connect_timeout 30
? router_id LVS_DEVEL
}
vrrp_instance VI_1 {
? ?state MASTER
? ?interface eth0
? ?virtual_router_id 51
? ?mcast_src_ip 192.168.101.77
? ?priority 100
? ?advert_int 1
? ?authentication {
? ? ? ?auth_type PASS
? ? ? ?auth_pass testpass
? ?}
? ?virtual_ipaddress {
? ? ? ?10.10.10.8
? ?}
}
nginx 配置
[root@open-source conf]# cat nginx.conf
user ?www;
worker_processes ?4;
worker_rlimit_nofile 65535;
error_log ? /var/log/lbnginx/error.log;
#error_log ?logs/error.log ?info;
#pid ? ? ? ?logs/nginx.pid;
events {
? ? ? ?use epoll;
? ? ? ?worker_connections ?102400;
}
http {
? ? ? ?include ? ? ? mime.types;
? ? ? ?default_type ?application/octet-stream;
? ? ? ?sendfile ? ? ? ?on;
? ? ? ?tcp_nopush ? ? on;
? ? ? ?tcp_nodelay ? ? off;
? ? ? ?gzip ?on;
? ? ? ?gzip_comp_level 1;
? ? ? ?gzip_proxied any;
? ? ? ?gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript
? ? ? ?keepalive_timeout ?30;
? ? ? ?log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';
#for sites enabled
? ? ? ?include ?/usr/local/app/lbnginx/conf/lb-enabled/*;
}
一個測試server的配置文件
#gy wwww.mytest.com
upstream mytest10000 {
# simple round-robin
? ? ? server 192.168.101.77:10000;
? ? ? server 192.168.101.88:10000;
}
server {
? ? ? ?listen 80;
? ? ? ?server_name mytest.com;
? ? ? ?access_log /var/log/lbnginx/mytest_access.log access;
? ? ? ?location ~ / {
? ? ? ? ? ? ? ?proxy_redirect off;
? ? ? ? ? ? ? ?proxy_set_header Host $host;
? ? ? ? ? ? ? ?proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ? ? ? ?proxy_set_header REMOTE-HOST $remote_addr;
? ? ? ? ? ? ? ?proxy_set_header HTTP_HOST $server_name;
? ? ? ? ? ? ? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? ? ?client_max_body_size 50m;
? ? ? ? ? ? ? ?client_body_buffer_size 256k;
? ? ? ? ? ? ? ?proxy_connect_timeout 30;
? ? ? ? ? ? ? ?proxy_send_timeout 30;
? ? ? ? ? ? ? ?proxy_read_timeout 60;
? ? ? ? ? ? ? ?proxy_buffer_size 256k;
? ? ? ? ? ? ? ?proxy_buffers 4 256k;
? ? ? ? ? ? ? ?proxy_busy_buffers_size 256k;
? ? ? ? ? ? ? ?proxy_temp_file_write_size 256k;
? ? ? ? ? ? ? ?proxy_next_upstream error timeout invalid_header http_500 http_503;
? ? ? ? ? ? ? ?proxy_max_temp_file_size 128m;
? ? ? ? ? ? ? ?proxy_pass http://mytest10000;
? ? ? ?}
}
測試
本機添加一個10.10.10.9的地址
安裝過程中報錯及解決辦法
1. # ./configure --prefix=/usr/local/app/lbnginx
錯誤提示PCRE庫
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
安裝pcre
[root@open-source src]# tar jxvf pcre-8.20.tar.bz2 ^C ? ? ?
[root@open-source src]# cd pcre-8.20^C ? ? ? ? ? ? ? ? ? ? ?
[root@open-source src]# cd pcre-8.20 ? ? ? ? ? ? ? ? ? ? ? ?
[root@open-source pcre-8.20]# ./configure --prefix=/usr/local/pcre
# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/local/pcre/
還是PCRE報錯
[root@open-source nginx-1.5.6]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/src/nginx-1.5.6'
cd /usr/local/pcre/lib/ \
? ? ? ?&& if [ -f Makefile ]; then make distclean; fi \
? ? ? ?&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
? ? ? ?./configure --disable-shared
/bin/sh: line 2: ./configure: No such file or directory
make[1]: *** [/usr/local/pcre/lib//Makefile] Error 127
make[1]: Leaving directory `/usr/src/nginx-1.5.6'
make: *** [build] Error 2
google了一下應該制定pcre源碼目錄
[root@open-source nginx-1.5.6]# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/src/pcre-8.20
[root@open-source nginx-1.5.6]#make && make install
安裝keepalived
[root@open2 src]# tar zxvf keepalived-1.2.8.tar.gz ? ? ? ? ?
[root@open2 src]# cd keepalived-1.2.8 ?
[root@open-source keepalived-1.2.8]# ./configure --prefix=/usr/local/app/keepalived
2. ? ?# ./configure --prefix=/usr/local/app/keepalived
報錯
configure: error: Popt libraries is required
[root@open-source keepalived-1.2.8]# yum -y install popt popt-devel ? ?
[root@open-source keepalived-1.2.8]# ./configure --prefix=/usr/local/app/keepalived
[root@open-source keepalived-1.2.8]# make && make install
轉載于:https://blog.51cto.com/leeforget/1338494
總結
以上是生活随笔為你收集整理的nginx+keepalived安装配置(整理中)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVN的使用(服务端与客户端)
- 下一篇: Win Srv 2012 R2 RDS移