掌握nginx集群
安装nginx
tar -xvf 解压文件 -C 目标地址
依赖 gcc openssl-devel pcre-devel zlib-devel
安装:yum install gcc openssl-devel pcre-devel zlib- devel
创建用户和用户组。为了方便nginx运行而不影响 linux安全
创建组:groupadd -r nginx
创建用户:useradd -r -g nginx -M nginx
-M 表示不创建用户的家目录。
.安装Nginx
./configure
–prefix=/usr
.make && make install
配置Nginx为系统服务,以方便管理
1、在/etc/rc.d/init.d/目录中建立文本文件nginx
2、在文件中粘贴下面的内容:
#!/bin/sh
nginx - this script starts and stops the nginx daemon
chkconfig: - 85 15
description: Nginx is an HTTP(S) server, HTTP(S) reverse \
proxy and IMAP/POP3 proxy server
processname: nginx
config: /etc/nginx/nginx.conf
config: /etc/sysconfig/nginx
pidfile: /var/run/nginx.pid
Source function library.
. /etc/rc.d/init.d/functions
Source networking configuration.
. /etc/sysconfig/network
Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
make required directories
user=nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -
options=$nginx -V 2>&1 | grep 'configure arguments:'
for opt in $options; do
if [ echo $opt | grep '.*-temp-path'
]; then
value=echo $opt | cut -d "=" -f 2
if [ ! -d “$value” ]; then
# echo “creating” $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c
N
G
I
N
X
C
O
N
F
F
I
L
E
r
e
t
v
a
l
=
NGINX_CONF_FILE retval=
NGINXCONFFILEretval=?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc
p
r
o
g
−
Q
U
I
T
r
e
t
v
a
l
=
prog -QUIT retval=
prog−QUITretval=?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc
n
g
i
n
x
−
H
U
P
R
E
T
V
A
L
=
nginx -HUP RETVAL=
nginx−HUPRETVAL=?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case “$1” in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $“Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac
3、修改nginx文件的执行权限
chmod +x nginx
4、添加该文件到系统服务中去
chkconfig --add nginx
查看是否添加成功
chkconfig --list nginx
启动,停止,重新装载
service nginx start|stop
#设定虚拟主机配置
server {
#侦听80端口
listen 80;
#定义使用 www.nginx.cn访问
server_name www.nginx.cn;
#定义服务器的默认网站根目录位置
root html;
#设定本虚拟主机的访问日志
access_log logs/nginx.access.log main;
#默认请求 ,Location配置请求映射规则
location / {
root html; #html:表示一个目录的相对路径。以tengine的跟目录为起点
#定义首页索引文件的名称
index index.php index.html index.htm;
}
# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#禁止访问 .htxxx 文件
location ~ /.ht {
deny all;
}
}
}
htttp{
server{
#表示一个虚拟主机
}
}
location 映射
location [ = | ~ | ~* | ^~ ] uri { … }
location URI {}:
对当前路径及子路径下的所有对象都生效;
location = URI {}: 必须执行一个具体文件路径(不能使用目录)
精确匹配指定的路径,不包括子路径,因此,只对当前资源生效;
location ~ URI {}:
location ~* URI {}:
模式匹配URI,此处的URI可使用正则表达式,区分字符大小写,不区分字符大小写;
location ^~ URI {}:
不使用正则表达式
优先级:= > ^~ > | > /|/dir/
配置session共享如下:
拷贝jar到tomcat的lib下,jar包见附件
配置tomcat,每个tomcat里面的context.xml中加入
⦁ <Manager className=“de.javakaffee.web.msm.MemcachedBackupSessionManager”
⦁ memcachedNodes=“n1:192.168.9.11:11211”
⦁ sticky=“false”
⦁ lockingMode=“auto”
⦁ sessionBackupAsync=“false”
⦁ requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
⦁ sessionBackupTimeout=“1000” transcoderFactoryClass=“de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory”
⦁ />
tengine新增会话保持功能:
在upstream 里面增加一行配置:
upstream laoxiao {
server IP:8009 weight=5;
server IP:8009 weight=5;
}
location ~ .jsp$ {
proxy_pass http://laoxiao;
}