nginx和tomcat實現動靜分離,加快訪問網站的速度。工做流程就是 lvs---->keepalive---->nginx負載均衡---->tomcat準備四臺服務器:1. web1 :192.168.4.102. web2 :192.168.4.113. keep_master :192.168.4.204. kepp_slave :192.168.4.21web1和web2上: (web2同樣)nginx安裝過程省略...1. 配置ngnix[root@web1 opt]# vim /usr/local/nginx/conf/nginx.conf user nginx nginx;worker_processes 1;error_log logs/error.log info;pid logs/nginx.pid;worker_rlimit_nofile 65535;events { worker_connections 1024; use epoll;}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"'; access_log logs/access.log main; #tcp_nopush on; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; server_tokens off; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_send_timeout 60; proxy_read_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; #keepalive_timeout 0; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x -javascript text/css application/xml; gzip_vary on; server { listen 80; server_name www.test123.com; index index.html index.htm index.jsp index.do; root /var/www/html; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ .(jsp|jspx|do)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://127.0.0.1:8080; } location ~ ..(htm|html|gif|jpg|jpeg|png|bmp|swf|zip|rar|doc|mp3|pdf) { expires 30d; } location ~ ..(js|css)?$ { expires 1h; } }開啓nginx[root@web1 opt]# /usr/local/nginx/sbin/nginx[root@web2 opt]# /usr/local/nginx/sbin/nginx[root@web1 opt]#echo "192.168.4.10" >/var/www/html/index.html[root@web2 opt]#echo "192.168.4.12" >/var/www/html/index.htm安裝tomcat 安裝過程省略...2 . 配置tomcat(web2同樣)[root@web1 opt]# vim /usr/local/tomcat/conf/server.xml
<Host name="www.test123.com" appBase="a" unpackWARs="true" autoDeploy="true">
3 .建立jsp文件,測試
[root@web1 opt]#vim /data/tomcatweb/111.jsp
Now time is: <%=new>javascript
[root@web2 opt]#vim /data/tomcatweb/111.jsp
Now time is: <%=new>
4.[root@web1 opt]#/usr/local/tomcat/bin/startup.sh
[root@web2 opt]#/usr/local/tomcat/bin/startup.sh
5.編寫realserver.sh腳本,在lo網卡上綁定VIP,設定ARP抵制功能
[root@web1 opt]# cat realserver.sh
#!/bin/bash
snf_vip=192.168.4.100
case $1 in
start)
ifconfig lo:0 $snf_vip netmask 255.255.255.255 broadcast $snf_vip
/sbin/route add -host $snf_vip dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &>/dev/null
echo -e "\033[32m start OK\033[0m"
;;
stop)
ifconfig lo:0 down
/sbin/route del $snf_vip &>/dev/null
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &>/dev/null
echo -e "\033[32m stopd OK\033[0m"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0
6 . 運行腳本。
[root@web1 opt]#./realserver.sh start
[root@web2 opt]#./realserver.sh start
7 . keep_master和keep_slave上安裝ipvsadm和keepalive(keep_slave同樣)
[root@ keep_master LoadBalancer]# rpm -ivh ipvsadm-1.26-4.el6.x86_64.rpm
[root@lkeep_master ~]# yum install -y gcc kernel-devel openssl-devel popt-devel
[root@lkeep_master ~]# tar -xzf keepalived-1.2.7.tar.gz
[root@lkeep_master ~]# cd keepalived-1.2.7
[root@lkeep_master keepalived-1.2.7]# ./configure --sysconf=/etc
[root@lkeep_master keepalived-1.2.7]# make && make install
[root@lkeep_master keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /sbin/
[root@lkeep_master keepalived-1.2.7]# chkconfig keepalived on
[root@keep_master opt]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {css
zte_lwz1@126.com
}
notification_email_from br/>keep_lvs@mail.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.100
}
}
virtual_server 192.168.4.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.4.10 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.4.11 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
在keep_slave上只需修改
state MASTER ------>state SLAVE
router_id lvs1 ------> router_id lvs2
priority 100 ------> priority 80html
[root@keep_master opt]#/etc/init.d/keepalived start
[root@keep_slave opt]#/etc/init.d/keepalived startjava