LVS群集 nginx+tomcat
项目拓扑图:
一.安装nginx + tomcat
192.168.1.248和192.168.1.249服务器上如下配置:
1. JDK的安装
将下载好的安装程序放到/soft目录下
cd /soft
chmod a+x jdk-6u23-linux-i586.bin
./jdk-6u23-linux-i586.bin
mkdir -p /data/conf
mv jdk1.6.0_23/ /data/conf/jdk
删除系统中旧的版本
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
创建链接,设置新的版本
ln -s /data/conf/jdk/bin/java /usr/bin/java
ln -s /data/conf/jdk/bin/javac /usr/bin/javac
查看新的版本信息
java -version
javac -version
查看jdk版本是否是1.6.到此JDK已经安装完成
2. Tomcat安装
解压压缩文件
cd /soft
tar xzvf apache-tomcat-6.0.32.tar.gz
mv apache-tomcat-6.0.32 /data/conf/tomcat
cd /data/conf/tomcat/bin/
添加用户
useradd webuser -s /sbin/nologin
chown -R webuser:webuser /data/
tomcat优化选项
(1)添加tomcat管理员
# vi /usr/local/www/tomcat/conf/tomcat-users.xml
在<tomcat-users> </tomcat-users>中间添加
<role rolename="manager"/>
<user username="tomcat" password="li147258369" roles="manager"/>
其中username="tomcat"为用户名password="li147258369"为密码
(2)修改JVM
JAVA_OPTS="-Xms1024m -Xmx1024m -Xmn256m -Djava.awt.headless=true"
(3)server.xml 参数修改
<Connector port="8080" protocol="HTTP/1.1"
maxHttpHeaderSize="8192" useBodyEncodingForURI="true"
maxThreads="600" 最大连接数
redirectPort="8443"
enableLookups="false" 禁用DNS查询
compression="on"
compressionMinSize="2048" 压缩,压缩大小
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
disableUploadTimeout="true"
/>
设置环境目录
vi /etc/profile
TOMCAT_HOME=/data/conf/tomcat
JAVA_HOME=/data/conf/jdk
JRE_HOME=/data/conf/jdk/jre
export JAVA_HOME JRE_HOME TOMCAT_HOME
备份tomcat配置文件
cd /data/conf/tomcat/conf
mv server.xml server.xml.bak
设置tomcat配置文件,配置虚拟主机
vi server.xml
在</Host>后面添加
<Host name="www.benet.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.benet.com" />
</Host>
<Host name="www.accp.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.accp.com" />
</Host>
<Host name="www.apache.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.apache.com" />
</Host>
启动tomcat
/data/conf/tomcat/bin/startup.sh
查看启动进程
ps -ef |grep tomcat
访问tomcat测试页,出现猫头网页,则安装成功!
links http://localhost:8080
3. 安装nginx
解压压缩文件
tar zxvf pcre-8.02.tar.gz
cd pcre-8.02/
编译安装
./configure
make && make install
tar zxvf nginx-0.8.50.tar.gz
cd nginx-0.8.50/
编译安装
./configure --prefix=/data/conf/nginx --with-http_stub_status_module
make && make install
配置nginx配置文件
cd /data/conf/nginx
备份nginx.conf配置文件
mv nginx.conf nginx.conf.bak
vi nginx.conf
user nobody nobody;
worker_processes 4;
pid /data/conf/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
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 www
{
server 192.168.1.248:8080;
server 192.168.1.249:8080;
}
server {
listen 80;
server_name www.benet.com;
location / {
root /data/web/www.benet.com ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/benet.com/www.benet.access.log;
error_log /data/logs/benet.com/error-www.benet.com;
}
server {
listen 80;
server_name www.accp.com;
location / {
root /var/www ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/accp.com/www.accp.access.log;
error_log /data/logs/accp.com/error-www.accp.com;
}
server {
listen 80;
server_name www.apache.com;
location / {
root /data/web/www.apache.com ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/apache.com/www.apache.access.log;
error_log /data/logs/apache.com/error-www.apache.com;
}
}
检测配置文件是否存在错误
./nginx -t
启动nginx
创建项目目录
mkdir /data/web/www.benet.com
mkdir /data/web/www.accp.com
mkdir /data/web/www.apache.com
mkdir -p /data/logs/benet.com
mkdir -p /data/logs/accp.com
mkdir -p /data/logs/apache.com
建立测试网页
echo "This is benet home page !!!" > /data/web/www.benet.com/index.html
echo "This is accp home page !!! " > /data/web/www.accp.com/index.html
echo "This is apache home page !!!" > /data/web/www.apache.com/index.html
添加hosts记录
echo "192.168.1.248 www.benet.com" >> /etc/hosts
echo "192.168.1.248 www.accp.com" >> /etc/hosts
echo "192.168.1.248 www.apache.com" >> /etc/hosts
浏览测试网页,出现相应的网页,则配置完成。
links http://www.benet.com
links http://www.accp.com
links http://www.apache.com
2011年04月07日修改完成
4. sersync 同步配置,使得网站项目文件一致。
在192.168.1.248服务器上安装rsync
tar xzvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/data/conf/rsync
make && make install
rpm -qa |grep rsync
rpm -qf /usr/bin/rsync
rpm -e rsync-2.6.8-3.1
ln -s /data/conf/rsync/bin/rsync /usr/bin/rsync
查看rsync版本
rsync -version
配置rsync配置文件
vi rsyncd.conf
port = 873
max connections = 4
pid file = /data/conf/rsync/rsync.pid
lock file = /data/conf/rsync/rsync.lock
log file = /data/conf/rsync/rsync.log
[dataweb]
path = /data/web
auth users = aa
secrets file = /data/conf/rsync/conf/rsync.passwd
uid = webuser
gid = webuser
read only = no
创建认证用户名和密码文件
vi /data/conf/rsync/conf/rsync.passwd
aa:123456
启动rsync守护进程
rsync --daemon
配置sersync配置文件
vi confxml.xml
修改为<localpath watch="/data/web">
<remote ip="192.168.1.248" name="dataweb"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="aa" passwordfile="/data/conf/rsync/conf/rsync.passwd"/>
<userDefinedPort start="false" port="873"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
开启sersync守护进程
./sersync2 -d
测试文件是否同步
在192.168.1.249服务器上/data/web/目录下创建些文件
在192.168.1.248服务器上/data/web/目录下看是否同步有相应的文件
LVS群集配置
1.客户端 就是真实服务器
在192.168.1.248和249服务器环回接口上绑定 虚拟ip
vi /data/conf/realserver
#!/bin/bash
#description : start realserver
VIP=192.168.1.100
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
开启虚拟IP
cd /data/conf
./realserve start
查看虚拟IP
ip add 或者 ifconfig
192.168.1.246 lvs主机------------192.168.1.247 lvs备份机
1. 在1.246和247上安装 ipvsadm
yum -y install ipvsadm
2. 192.168.1.246 lvs主机安装keepalived
安装keepalived
cd /soft
tar xzvf keepalived-1.1.19_.tar.gz
./configure --prefix=/data/conf/keepalived
make
make install
创建keepalived目录
mkdir -p /etc/keepalived
创建keepalived.conf配置文件
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 #(如果有多个VIP,继续换行填写.)
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
inhibit_on_failure #当web挂掉的时候,前面请求的用户,可以继续打开页面,但是后面的请求不会调度到挂掉的web上面。
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.1.248 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.249 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
启动keepalived
cd /data/conf/keepalived/sbin
./keepalived -D
查看keepalived进程,是否有3个进程
ps -ef |grep keepalived
查看ipvsadm
ipvsadm -L -c
LVS群集测试:
设置hosts文件
192.168.1.100 www.benet.com
192.168.1.100 www.accp.com
192.168.1.100 www.apache.com
访问测试网页
http://www.benet.com
关闭LVS master主机,看是否还能访问测试网页不,如果能访问,则LVS配置成功了,若不行,则进行相应的排错啦.
本文出自 “fallenleaves” 博客,请务必保留此出处http://fallenleaves.blog.51cto.com/1383716/530901
Apache,tomcat,nginx,apache+tomcat,nginx+tomcat自动化安装脚本
#!/bin/bash
#Auto Install Apache Tomcat Nginx apache+tomcat nginx+tomcat.
#apache version: 2.2.17 download: http://httpd.apache.org/download.cgi#apache22
#jdk version: 1.6 download: http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
#tomcat version: 6.0.23 download: http://tomcat.apache.org/download-60.cgi
#nginx version: 0.8.54 download: http://nginx.org/en/download.html
#执行这个自动化安装脚本需要把apache tomcat jdk nginx的安装包拷贝到/soft目录下.
#apache安装在/opt/conf/apache目录下. nginx安装在/opt/conf/nginx目录下.
#tomcat安装在/opt/conf/tomcat目录下. jdk安装在/opt/conf/jdk目录下.
# 20110413 write: liweizhong e-mail:[email protected] QQ:543302969 Version 1.1
IN_SRC=/soft
IN_DIR=/opt/conf
echo "=================Check software development environment.================"
echo ""
rpm -q gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel > /dev/null
if [ "$?" != 0 ] ;
then
echo "Please check your need software"
exit 0
else
echo "software is OK !! "
fi
if [[ ! -d $IN_DIR ]]
then
mkdir -p $IN_DIR
fi
function apache_ins {
echo ""
echo "===========================Apache installing============================="
sleep 5
#Auto apache
echo "installing httpd..."
cd $IN_SRC
tar jxvf httpd-2.2.17.tar.bz2
cd httpd-2.2.17
./configure --prefix=$IN_DIR/apache --with-mpm=worker --enable-rewrite --enable-deflate --disable-userdir --enable-so
[ $? != 0 ] && exit
make
[ $? != 0 ] && exit
make install
[ $? != 0 ] && exit
echo "Include conf/vhost.conf" >> $IN_DIR/apache/conf/httpd.conf
sed -i "s/#ServerName/ServerName localhost/g" $IN_DIR/apache/conf/httpd.conf
touch $IN_DIR/apache/conf/vhost.conf
$IN_DIR/apache/bin/apachectl start
echo "$IN_DIR/apache/bin/apachectl start" >> /etc/rc.local
${SERVICES}_ins
sleep 2
echo "==========================Auto Install finished=========================="
}
function tomcat_ins {
echo ""
echo "===========================Tomcat installing============================="
sleep 5
#Auto Install JDK
cd /soft
chmod 700 jdk*
./jdk-6u23-linux-i586.bin
mv jdk1.6.0_23/ /opt/conf/jdk
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
ln -s /opt/conf/jdk/bin/java /usr/bin/java
ln -s /opt/conf/jdk/bin/javac /usr/bin/javac
java -version
sleep 3
javac -version
echo ""
echo ""
echo "====================Auto Install JDK Have finished======================"
#Auto Install tomcat
sleep 5
cd /soft
tar xzvf apache-tomcat-6.0.32.tar.gz
mv apache-tomcat-6.0.32 /opt/conf/tomcat
cat >> /etc/profile << EFF
TOMCAT_HOME=/opt/conf/tomcat
JAVA_HOME=/opt/conf/jdk
JRE_HOME=/opt/conf/jdk/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$TOMCAT_HOME/bin/
export JAVA_HOME JRE_HOME PATH TOMCAT_HOME
EFF
source /etc/profile
sleep 3
cd /opt/conf/tomcat/bin
./startup.sh
echo ""
echo ""
echo "=================Already all installation is complete================="
}
function nginx_ins {
echo ""
echo "===========================Nginx installing==========================="
sleep 5
#Auto nginx
echo "installing nginx..."
cd $IN_SRC
tar zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54
make_clean
useradd www
./configure --user=www --group=www --prefix=$IN_DIR/nginx --without-http_rewrite_module --with-http_stub_status_module --with-http_ssl_module
[ $? != 0 ] && exit
make
[ $? != 0 ] && exit
make install
[ $? != 0 ] && exit
/opt/conf/nginx/sbin/nginx
echo "$IN_DIR/nginx/sbin/nginx " >> /etc/rc.local
${SERVICES}_ins
sleep 3
echo "===============Auto Install finished================================="
}
echo "Slect install
1. apache
2. tomcat+jdk
3. nginx
4. apache+tomcat
5. nginx+tomcat
6. don't install is now
"
read -p "Please Input 1,2,3,4,5,6:" SERVER_ID
if [[ $SERVER_ID == 5 ]]; then
SERVICE="nginx"
SERVICES="tomcat"
elif [[ $SERVER_ID == 4 ]]; then
SERVICE="apache"
SERVICES="tomcat"
elif [[ $SERVER_ID == 3 ]]; then
SERVICE="nginx"
elif [[ $SERVER_ID == 2 ]]; then
SERVICE="tomcat"
elif [[ $SERVER_ID == 1 ]]; then
SERVICE="apache"
else
echo ""
echo "Thanks bye!!"
exit
fi
${SERVICE}_ins
本文出自 “fallenleaves” 博客,请务必保留此出处http://fallenleaves.blog.51cto.com/1383716/545832
自动安装多个tomcat shell脚本
自动安装多个tomcat shell脚本
#!/bin/bash
#Auto Install JDK、tomcat and they connector.
#执行这个脚本需要在/soft目录下放好JDK、tomcat、的tar.gz源码包,
#以及已经执行过的jdk(本人无法做到在jdk执行时输入Enter和空格,所以这一步骤需要手动做。)
#tomcat安装到/data/conf/tomcat,已设置环境变量。
#JDv安装>/data/conf/jdk,已设置环境变量。
# 20110401 [email protected] Version 1.0 write:fallenleaves QQ:543302969
echo "===========================Began installing============================="
if [ ! -d /opt/conf ]
then
mkdir -p /opt/conf
else
echo "This directory is already exists"
fi
#Auto Install JDK
cd /soft
chmod 700 jdk*
./jdk-6u24-linux-i586.bin
mv jdk1.6.0_24/ /opt/conf/jdk
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
ln -s /opt/conf/jdk/bin/java /usr/bin/java
ln -s /opt/conf/jdk/bin/javac /usr/bin/javac
java -version
sleep 5
javac -version
echo ""
echo ""
echo "====================Auto Install JDK Have finished======================"
#Auto Install tomcat
for i in `seq 1 3`
do
mkdir -p /opt/conf/tomcat$i
cd /soft
tar xzvf apache-tomcat-7.0.12.tar.gz
mv apache-tomcat-7.0.12/* /opt/conf/tomcat$i
cat >> /etc/profile << EFF
TOMCAT$i=/opt/conf/tomcat$i
EFF
done
cat >> /etc/profile << FFG
JAVA_HOME=/opt/conf/jdk
JRE_HOME=/opt/conf/jdk/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$TOMCAT1/bin:$TOMCAT2/bin:$TOMCAT3/bin/
export JAVA_HOME JRE_HOME PATH TOMCAT1 TOMCAT2 TOMCAT3
FFG
source /etc/profile
本文出自 “fallenleaves” 博客,请务必保留此出处http://fallenleaves.blog.51cto.com/1383716/547974
nginx+tomcat负载均衡缓存服务器集群
Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。
最新的Nginx 0.8.32版本,proxy_cache和fastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid。我们已经在生产环境使用了 Nginx 的 proxy_cache 缓存功能超过两个月,十分稳定,速度不逊于 Squid。
在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。
http://surpassdream.blog.51cto.com/1347340/544270
http://wgkgood.blog.51cto.com/
nginx rewrite 参数和例子
http://www.cnblogs.com/analyzer/articles/1377684.html
]
本位转自:http://blog.c1gstudio.com/archives/434
推荐参考地址:
Mailing list ARChives 官方讨论区
http://marc.info/?l=nginx
Nginx 常见应用技术指南[Nginx Tips]
http://bbs.linuxtone.org/thread-1685-1-1.html
本日志内容来自互联网和平日使用经验,整理一下方便日后参考。
正则表达式匹配,其中:
文件及目录匹配,其中:
flag标记有:
一些可用的全局变量有,可以用做条件判断(待补全)
结合QeePHP的例子
多目录转成参数
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
目录对换
/123456/xxxx -> /xxxx?id=123456
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
目录自动加“/”
禁止htaccess
禁止多个目录
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
禁止单个目录
不能禁止.log.txt能请求
禁止单个文件
给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志
设定某个文件的过期时间;这里为600秒,并不记录访问日志
文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
只充许固定ip访问网站,并加上密码
将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html
将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/
上面例子有个问题是访问/shanghai 时将不会匹配
这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果
知道原因后就好办了,让我手动跳转吧
文件和目录不存在的时候重定向:
域名跳转
多域名转向
三级域名跳转
域名镜向
某个子目录作镜向
discuz ucenter home (uchome) rewrite
discuz 7 rewrite
给discuz某版块单独配置域名
discuz ucenter 头像 rewrite 优化
jspace rewrite