目录
Nginx安装 (异步非阻塞)
安装tengine安装依赖: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/tengine-2.1make && make install配置Nginx为系统服务,以方便管理在/etc/rc.d/init.d/目录中建立文本文件nginx在文件中粘贴下面的内容: 注意:nginx是sbin下的nginx启动脚本NGINX_CONF_FILE是conf下的配置文件(ps -fe |grep nginx)#!/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 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval} stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval} restart() {configtest || return $?stopsleep 1start} reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $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" instart)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 2esac
修改nginx文件的执行权限chmod +x nginx添加该文件到系统服务中去 chkconfig --add nginx 查看是否添加成功 chkconfig --list nginx启动,停止,重新装载 service nginx start|stopokNginx的session共享
使用mencached作为session存储yum -y install memcachedmemcached -d -m 128m -p 11211 -l 192.168.9.11 -u root -P /tmp/ 修改tomcat的conf/comtext.xml文件如下 (ip地址使用memcached所在的服务器上 , 其他需要session共享的全部都要修改)<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" />每个需要session共享的tomcat都拉jar包进去lib中修改更改 webapps/ROOT/index.jsp 为 : form 192.168.226.17session=<%=session.getId()%>tengine的反向代理及轮询upstream bula{server 192.168.226.19:8080;server 192.168.226.20:8080;#server 192.168.226.17:8080;} server {listen80; server_name www.lqy.com;location /{proxy_pass http://bula;} } 测试
nginx实现动静分离
17服务请向19服务器请求.jsp动态数据<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>动静分离的测试</title> </head> <body> <div>result</div><br><img src="up3.png"> </body> </html>nginx的配置文件设置 server {listen80;server_name www.kkk.com;#这里是静态数据访问location /{root /opt/sxt;}#这里是动态数据访问location ~* \.(jsp|do|action|asp|php)$ {proxy_pass http://192.168.226.19:8080;}}测试