简单描述一下nginx两块网卡,eth1外网,eth0内网与tomcat eth0通信,用户请求静态内容由nginx直接响应,动态内容则nginx转发至后端tomcat响应

安装nginx,tomcat不再描述。

nginx的配置文件如下:/etc/nginx/nginx.conf

[root@node5 ~]# pnginxworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  localhost;        location / {            root   html;            index  index.html index.htm;        }    location ~* \.(jsp|do)$ {        proxy_pass http://172.16.251.23:8080;    }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

访问测试,把相应的样式表,图片复制到nginx页面目录即可如下图:

以下介绍apache + tomcat 反向代理设置

1,介绍,apache(mod_proxy)+tomcat(安装配置简单,动静分离相对复杂,简单介绍反代动态资源)

       tomcat安装不再描述。

   安装apache    

yum install httpd -y

   通过http协议进行对后端的服务器的代理,配置如下:    

      新建并编配置文件: vim /etc/httpd/conf.d/mod_proxy.conf

[root@node5 conf.d]# cat mod_proxy.confProxyVia onProxyRequests offProxyPreserveHost onProxyPass / http://172.16.251.24:8080/ProxyPa***everse / http://172.16.251.24:8080/
Order Allow,Deny Allow from all

   也可以通过ajp协议进行对后端的服务器的代理,配置如下:

      新建并编配置文件: vim /etc/httpd/conf.d/mod_proxy.conf

[root@node5 conf.d]# cat mod_proxy.confProxyVia onProxyRequests offProxyPreserveHost onProxyPass / ajp://172.16.251.24:8009/ProxyPa***everse / ajp://172.16.251.24:8009/
Order Allow,Deny Allow from all

分别启动tomcat ,apache进行测试

2,介绍apache(mod_jk)+tomcat(ajp connectors) 代理通信的简单配置

   安装apache

[root@node5 conf.d]yum install httpd httpd-devel -y[root@node5 software]# tar xf tomcat-connectors-1.2.37-src.tar.gz[root@node5 software]# cd tomcat-connectors-1.2.37-src/native/[root@node5 native]# pwd/software/tomcat-connectors-1.2.37-src/native[root@node5 native]# which apxs/usr/sbin/apxs[root@node5 native]# ./configure --with-apxs=/usr/sbin/apxs[root@node5 native]#make && make install

   查看安装下的connector module

编辑文件 /etc/httpd/conf.d/mod_jk.conf

[root@node5 conf.d]# cat mod_jk.confLoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel noticeJkMount /* Node4_TomcatAJkMount /status stat1

编辑文件 /etc/httpd/conf.d/workers.properties

[root@node5 conf.d]# cat workers.properties.bkworker.list=Node4_TomcatA,stat1worker.Node4_TomcatA.type=ajp13worker.Node4_TomcatA.port=8009worker.Node4_TomcatA.host=172.15.251.24worker.Node4_TomcatA.lbfactor=1worker.stat1.type = status

配置完成进行测试

截图如下所示:

完成!

基于apache(mod_jk)+tomcat的负载均衡集群的配置(架构图如上所示)

此架构对上一架构的补充,只是后端增加了一台或多台tomcat,前端apache中的配置文件修改为如下

编辑文件 /etc/httpd/conf.d/mod_jk.conf

LoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel notice#JkLogLevel debugJkMount /* lbcluster1JkMount /jklbcstatus/ stat1

编辑文件 /etc/httpd/conf.d/workers.properties

worker.list=lbcluster1,stat1worker.Node4_TomcatA.type=ajp13worker.Node4_TomcatA.port=8009worker.Node4_TomcatA.host=172.15.251.24worker.Node4_TomcatA.lbfactor=1worker.Node3_TomcatB.type=ajp13worker.Node3_TomcatB.port=8009worker.Node3_TomcatB.host=172.15.251.23worker.Node3_TomcatB.lbfactor=1worker.lbcluster1.type = lbworker.lbcluster1.sticky_session = 1worker.lbcluster1.balance_workers = Node3_TomcatB,Node4_TomcatAworker.stat1.type = status

配置完成启动两台tomcat服务器,测试的结果还不能session保持

session会话保持的具体实现

   如上图中的架构中,在两台tomcat服务器上,分别创建,如下目录结构和文件

两台tomcat服务器中,index.jsp文件内容分别如下

<%@ page language="java" %>  Node4_TomcatB      

Node4_TomcatB

<% session.setAttribute("abc","abc"); %>
Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

<%@ page language="java" %>  Node3_TomcatA      

Node3_TomcatA

<% session.setAttribute("abc","abc"); %>
Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

两台tomcat服务器中,web.xml文件内容如下

   复制tomcat自带web.xml到相应的目录,并增加字符串:<distributalbe/>

两台tomcat服务器中,在server.xml文件engine段,增加内容如下:

   

两台tomcat服务器中,分别增加路由

   route add -net 228.66.0.4 netmask 255.255.255.255 dev eth0

设置完成分别启动tomcat 提示出现如下信息,说明配置成功

在前端http进行测试刷新页面,对应的sessid始终如一。