lfjgh 发表于 2015-8-10 09:52:19

Nginx+tomcat7的负载均衡和动静分离配置

简介:    所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片、html等静态的文件,tomcat(或weblogic)处理jsp、do等动态文件,从而达到动静页面访问时通过不同的容器来处理。nginx处理静态页面效率远高于tomcat,而tomcat擅长动态页面处理,这样一来就能更好的提高并发,处理性能。所谓负载均衡就是、采用nginx的proxy_pass将location做动静分离后的jsp、do等jsp程序文件分发到后端upstreamd模块中tomcat集群上,rewrite做正则分发,此时也将应用到nginx经典之处的IP哈希(ip_hash)模块,这样每个访客固定访问一个后端web服务器,可以解决session的问题;
   但采用ip_hash指令无法保证后端服务器的负载均衡,可能有些后端服务器接收的请求多,有些后端服务器收到的请求少,而且设置后端服务权重等方法将不起作用。所以,如果后端的动态应用服务器能够做到SESSION共享,还是建议采用后端服务的SESSION共享方式代替Nginx的ip_hash方式。(可使用memcached来保持session,实现session共享)架构描述一台nginx:做静态的html服务器和负载均衡两台tomcat服务器每台服务器装有2个tomcat服务,使其支持动态jsp程序和.do文件两台nginx服务器每台服务器装有2个nginx,支持PHP文件1. 安装JDK先用java命令查看系统是否安装了默认的openJDK,如果已安装,建议用yum remove java卸载。下载jdk:oracle的jdk安装:rpm –ivh jdk-7u79-linux-x64.rpm配置环境变量(等到tomcat安装好之后一起配置)
2. 安装tomcatTomca下载地址:http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.63/bin/apache-tomcat-7.0.63.tar.gz下载后解压:tar -zxvf apache-tomcat-7.0.63.tar.gz移动到/usr/local/tomcat:mv apache-tomcat-7.0.63 /usr/local/tomcat设置目录可执行权限 chmod +x /usr/local/tomcatcp -ar /usr/local/tomcat1/usr/local/tomcat2
#分别修改tomcat1和tomcat2 端口,这里有三个端口需要修改,分别如下:
shutdown 端口:8005主要负责启动关闭.
ajp端口:8009 主要负责通过ajp均衡(常用于apache和tomcat整合)
http端口:8080 可以通过web页面直接访问(nginx+tomcata整合)
#注* 如果tomcat1三个端口分别为:8005 8009 8080 ,那么tomcat2端口在此基础上都+1,即为:8006 8010 8081
#一台服务器上,端口不能重复,否则会报错。
#修改完端口后,然后启动两个tomcat,启动命令为:
#如何提示没有这个文件或者权限不足,需要tomcat 的bin目录对sh文件赋予执行权限:chmod o+x   *.sh
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
#启动后,使用netstat -atnl 可以看到6个端口,即代表tomcat1 tomcat2成功启动。
你可以使用 http://ip:8080   http://ip:8081访问tomcat默认页面。

#如果需要修改tomcat发布目录为自己制定的目录,需要做如下调整,创建两个发布目录:

mkdir -p /usr/webapps/{www1,www2}
编辑vi /usr/local/tomcat1/conf/server.xml 在最后</Host>前一行加下内容:<Contextpath="" docBase="/usr/webapps/www1" reloadable="false"/>
编辑vi /usr/local/tomcat2/conf/server.xml 在最后</Host>前一行加下内容:
<Contextpath="" docBase="/usr/webapps/www2" reloadable="false"/>
3.tomcat1发布目录内容:
1
2
3
4
5
6
<html>
<body>
<h1>TOMCAT_1 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

4.tomcat2发布目录内容:
1
2
3
4
5
6
<html>
<body>
<h1>TOMCAT_2 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

然后访问http://ip:8080、8081查看测试内容。
配置环境变量 编辑profile文件:vim /etc/profile在文件中添加以下代码:   export JAVA_HOME=/usr/java/jdk1.7.0_79   #根据自己的安装结果来填写目录export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/libexport PATH=$JAVA_HOME/bin:$PATHexport TOMCAT_HOME=/usr/local/tomcatexport CATALINA_HOME=/usr/local/tomcat保存文件退出编辑。 使设置立即生效:source /etc/profile
至此,tomcat已经搭建完毕,下面所述的步骤主要是对tomcat的相关设置。启动tomcat: sh startup.sh 或者 sh /usr/local/tomcat7/bin/startup.sh此时可在浏览器查看http://IP:8080,如果没有图形界面,则需要打开端口才可访问。打开防火墙端口命令如下:/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT #8080为端口/etc/rc.d/init.d/iptables save #保存/etc/init.d/iptables restart #重启防火墙使得立即生效也可以在/etc/sysconfig/iptables文件中增加一行信息,-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT查看防火墙状态的命令:/etc/init.d/iptables status关闭防火墙的命令:/etc/init.d/iptables stop
3. 安装nginx#cd/usr/local/src#wgethttp://nginx.org/download/nginx-1.6.2.tar.gz 新建nginx用户与组#groupadd -g 108-r nginx#useradd -u 108 -r -g 108 nginx #id nginx uid=108(nginx) gid=108(nginx) 组=108(nginx)首先安装 Nginx 编译安装时的相关依赖: # yum install -y pcre-devel openssl openssl-devel zlib-devel gd-devel # tar zxvf nginx-1.6.2.tar.gz # cd nginx-1.6.2./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module编译并安装
# make && make install
5、Nginx + tomcat整合整合主要是修改nginx.conf配置,给一个完整的nginx.conf线上配置,部分参数可以自己根据实际需求修改:user nginx nginx;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include       mime.types;
default_typeapplication/octet-stream;
fastcgi_intercept_errors on;
charsetutf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush   on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size512k;
proxy_connect_timeout    5;
proxy_read_timeout       60;
proxy_send_timeout       5;
proxy_buffer_size      16k;
proxy_buffers            4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length1k;
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;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
access_log/nginx/logs/access.log;

#这里为后端服务器cluster应用集群配置,根据后端实际情况修改即可,cluster_app为负载均衡名称,可以任意指定
#但必须跟server段 proxy_pass定义一致,否则不能转发后端的请求。
upstream cluster_tomcat {
   ip_hash;
   server   10.10.141.30:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.30:8081 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.31:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.31:8081 weight=1 max_fails=2 fail_timeout=30s;
}
#这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定
upstream cluster_nginx {
    ip_hash;
    server   10.10.141.40:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.40:8081 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.41:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.41:8081 weight=1 max_fails=2 fail_timeout=30s;
}

####www.test1.cn
server
{
    listen       80;
    server_namewww.test1.com;
    indexindex.html index.php index.htm index.jsp index.do default.do;
#配置发布目录为/data/www/wugk
    root/data/www/wugk;
#动态页面交给http://cluster_tomcat,也即我们之前在nginx.conf定义的upstream cluster_tomcat 均衡
    location ~ .*\.(jsp|do)?$
    {
         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://cluster_tomcat;
    }
    location ~ \.(php|php5)?$     {         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:cluster_nginx;    }
#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
    root /data/www/wugk;
    #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
    expires      3d;
    }
}
6、部署测试:#检查Nginx配置文件是否配置正确,提示Ok and successful表示正确,如下:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#启动Nginx服务
/usr/local/nginx/sbin/nginx
#查看Nginx进程是否启动
ps -ef |grep nginx

1.将测试域名www.test.com 解析到前置机
客户端使用浏览器http://www.test.com/index .*测试首页测试      
2.测试要求
测试页面分别针对前置机nginx的动静分离和到tomcat上负载均衡
3.分别在对应的WebServer 上制作测试页面,并用不同IE在客户端测试
①:index.html
This is a html test page, the server address is nginx
②:index.php
This is a php test page, the server address is nginx-php
③:index.jsp 和index.do只做测试页面,不做具体动态程序
This is a jsp test page, the server address is tomcat
This is a do test page, the server address is tomcat

页: [1]
查看完整版本: Nginx+tomcat7的负载均衡和动静分离配置