janneyabc 发表于 2018-12-5 12:14:59

Nginx负载均衡Tomcat、Resin实现动静分离

  说明:前端202.207.178.7安装Nginx,提供负载均衡器及静态页面;后端202.207.178.6安装Tomcat、Resin,提供多实例。有关tomcat多实例的配置详见本人上一篇博客:Tomcat安装配置
  http://10927734.blog.运维网.com/10917734/1874112
  一、编译安装并配置Nginx
  1、首先添加用户nginx,实现以之运行nginx服务进程
  # groupadd -r -g 108 nginx
  # useradd -r -g 108 -u 108 nginx
  2、将下载好的软件包解压并安装(我这里是nginx-1.4.7.tar.gz)
  # tar xf nginx-1.4.7.tar.gz
  # cd nginx-1.4.7
  
  接着开始编译和安装:
  # ./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid\
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre \
  --with-file-aio
  
  # make && make install
  
  报错时可能要求安装如下包,按需安装即可!
  # yum -y install pcre-devel
  # yum -y install gcc
  # yum -y install openssl-devel
  
  
  3、为nginx提供SysV init脚本:
  

  新建文件/etc/rc.d/init.d/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 6
  make_dirs
  echo -n $"Starting $prog: "
  daemon $nginx -c $NGINX_CONF_FILE
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $lockfile
  return $retval
  }
  
  stop() {
  echo -n $"Stopping $prog: "
  killproc $prog -QUIT
  retval=$?
  echo
  [ $retval -eq 0 ] && rm -f $lockfile
  return $retval
  }
  
  restart() {
  configtest || return $?
  stop
  sleep 1
  start
  }
  
  reload() {
  configtest || return $?
  echo -n $"Reloading $prog: "
  killproc $nginx -HUP
  RETVAL=$?
  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
  

  4、而后为此脚本赋予执行权限:
  # chmod +x /etc/rc.d/init.d/nginx
  

  5、添加至服务管理列表,并让其开机自动启动:
  # chkconfig --add nginx
  # chkconfig nginx on
  

  6、而后就可以启动服务并测试了:
  # service nginx start
  此时访问202.207.178.7可以访问到nginx的默认页面
  二、nginx负载均衡Tomcat,并实现动静分离(在202.207.178.7上。在此之前,应该先在后端安装了Tomcat多实例)
  1、编辑nginx配置文件,添加以下内容
  # vim /etc/nginx/nginx.conf
  upstream tomcat{
  

  server 202.207.178.6:8080 weight=1 max_fails=2 fail_timeout=30s;
  server 202.207.178.6:8081 weight=1 max_fails=2 fail_timeout=30s;
  server 202.207.178.6:8082 weight=1 max_fails=2 fail_timeout=30s;
  

  }
  server
  {
  listen       80;
  server_namewww.wuguangke.cn;
  index index.jsp index.html index.htm;
  #配置发布目录为/data/www;
  root/data/webapps/www;
  #动态页面交给http://tomcat,也即我们之前在nginx.conf定义的               upstream tomcat 均衡
  location /
  {
   #出错后跳到下一台
   proxy_next_upstream http_502 http_504 error                         timeout invalid_header;
   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://tomcat;
   expires      3d;
  }
  #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取
  location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
   {
  root /data/webapps/www;
  #expires定义用户浏览器缓存的时间为3天,如果静态页面不常
  更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
  expires      3d;
  }
  }
  2、创建静态页面目录和文件
  # mkdir -p /data/webapps/www
  # vim /data/webapps/www/index.html
  添加如下内容:
  This is nginx static
  3、配置本地域名解析(在进行访问测试的真机上)
  修改C:\Windows\System32\drivers\etc\hosts文件,添加如下一行
  202.207.178.7 www.fsy.cn
  3、重启nginx服务,进行访问测试
  # service nginx restart
  此时,访问http://www.fsy.cn/,会现后端3个tomcat实例负载均衡;访问http://www.fsy.cn/index.html会发现访问到前端nginx上的测试页,实现了动静分离!
  三、安装配置resin
  说明:
  Resin是CAUCHO公司的产品,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语言开发。
  resin 普通版本和pro版本主要区别是 pro支持缓存和负载均衡。pro因为有强大的cache功能,独立作为web服务器处理静态页面性能都可以和apache有一比。但普通版本独立作为web服务器性能就要差一些。当然可以使用apache+resin的方案借助apache的缓存功能提高性能。
  1、编译安装软件包
  1)安装所需环境:
  #yum -y install gcc
  # yum -y install openssl-devel
  2)安装Resin
  # tar xf resin-4.0.33.tar.gz
  # ./configure --prefix=/usr/local/resin --with-resin-log=/data/logs/resin/         --with-java-home=/usr/java/jdk1.6.0_21/
  # make && make install
  2、修改配置文件
  # cd /usr/local/resin/conf
  # vim resin.xml
  
  # vim resin.properties
  修改如下两项:
  app.http          : 8081
  
  web.http          : 8081
  3、提供主访问测试页面
  # vim /data/webapps/www3/index.jsp
  如下内容:
   RESIN_3 JSP Test Page
  
  
  
  4、停止tomcat3,启动resin进行访问测试
  # /usr/local/tomcat3/bin/shutdown.sh
  # /usr/local/resin/bin/resin.sh start
  此时访问http://202.207.178.6:8082/,会访问到我们自定义的resin,访问http://www.fsy.cn/,会实现2个Tomcat实例和一个resin负载均衡,访问http://www.fsy.cn/index.html,会看到自定义的静态页面
  

  欢迎批评指正!




页: [1]
查看完整版本: Nginx负载均衡Tomcat、Resin实现动静分离