Nginx+Tomcat 集群部署

摘要:
aNULL:!

1.Nginx + Tomcat 集群部署 简单配置

1 #user  nobody;
2 worker_processes  4;#工作进程的个数
3 
4 #error_log  logs/error.log;
5 #error_log  logs/error.log  notice;
6 #error_log  logs/error.log  info;
7 
8 #pid        logs/nginx.pid;
9 
10 
11 events {
12 worker_connections  1024;  #单个进程连接数
13 }
14 
15 
16 http {
17 include       mime.types;
18 default_type  application/octet-stream;
19 
20 #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
21 #                  '$status $body_bytes_sent "$http_referer" '
22 #                  '"$http_user_agent" "$http_x_forwarded_for"';
23 
24 #access_log  logs/access.log  main;
25 
26 sendfile        on;
27 #tcp_nopush     on;
28 
29 #keepalive_timeout  0;
30 keepalive_timeout  65;
31 
32 #gzip  on;
33 #配置反向代理配置
34 gzip on;
35 upstream netitcast.com{
36 # 定下请求ip_hash;
37 ip_hash;
38 server 127.0.0.1:8181 weight=1;#服务器配置 代理分配  weight:分配权重 down:不参与负载均衡
39 server 192.168.0.105:8181 weight=1;#
40 }
41 
42 server {
43 listen       8888;
44 server_name  localhost;
45 
46 #charset koi8-r;
47 
48 #access_log  logs/host.access.log  main;
49 
50 location / {
51 root   html;
52 index  index.html index.htm;
53 # 配置
54 proxy_pass http://netitcast.com;
55 }
56 
57 #error_page  404              /404.html;
58 
59 # redirect server error pages to the static page /50x.html
60 #
61 error_page   500 502 503 504  /50x.html;
62 location = /50x.html {
63 root   html;
64 }
65 
66 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
67 #
68 #location ~ .php$ {
69 #    proxy_pass   http://127.0.0.1;
70 #}
71 
72 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
73 #
74 #location ~ .php$ {
75 #    root           html;
76 #    fastcgi_pass   127.0.0.1:9000;
77 #    fastcgi_index  index.php;
78 #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
79 #    include        fastcgi_params;
80 #}
81 
82 # deny access to .htaccess files, if Apache's document root
83 # concurs with nginx's one
84 #
85 #location ~ /.ht {
86 #    deny  all;
87 #}
88 }
89 
90 
91 # another virtual host using mix of IP-, name-, and port-based configuration
92 #
93 #server {
94 #    listen       8000;
95 #    listen       somename:8080;
96 #    server_name  somename  alias  another.alias;
97 
98 #    location / {
99 #        root   html;
100 #        index  index.html index.htm;
101 #    }
102 #}
103 
104 
105 # HTTPS server
106 #
107 #server {
108 #    listen       443 ssl;
109 #    server_name  localhost;
110 
111 #    ssl_certificate      cert.pem;
112 #    ssl_certificate_key  cert.key;
113 
114 #    ssl_session_cache    shared:SSL:1m;
115 #    ssl_session_timeout  5m;
116 
117 #    ssl_ciphers  HIGH:!aNULL:!MD5;
118 #    ssl_prefer_server_ciphers  on;
119 
120 #    location / {
121 #        root   html;
122 #        index  index.html index.htm;
123 #    }
124 #}
125 
126 }

2.关于Session 的问题

2.1Tomcat 提供了Cluster 的组件实现session 复制 ,Clurster 的配置

    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="8">
      <!--复制方式:
        org.apache.catalina.ha.session.DeltaManager:复制所有的session 到所有的节点(节点太多,耗费资源)
        org.apache.catalina.ha.session.BackupManager:    

        
        expireSessionsOnShutdown:一个程序被关闭时候,是否要销毁所有session
        notifyListenersOnReplication:session复制和移动的时候通知
      -->
      <Manager className="org.apache.catalina.ha.session.DeltaManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"/>

      <Channel className="org.apache.catalina.tribes.group.GroupChannel">
        <Membership className="org.apache.catalina.tribes.membership.McastService"address="228.0.0.4"port="45564"frequency="500"dropTime="3000"/>
        <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"address="auto"port="4002"autoBind="100"selectorTimeout="5000"maxThreads="6"/>
        
        <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
          <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
        </Sender>
        <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
        <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
      </Channel>

      <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"filter=""/>
      <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

      <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"watchDir="/tmp/war-listen/"watchEnabled="false"/>

      <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
      <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
    </Cluster>

注意:使用Cluster的组件实现Session复制的时候,一定要在程序中web.xml加上这样一句告诉tomcat这个程序是分布式

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">
  <display-name>Test</display-name>
   <!--分布式
  -->
  <distributable/>
</web-app>

测试Session复制和Nginx集群是否成功页面

1 <%@ page contentType="text/html; charset=GBK" %>
2 <%@ page import="java.util.*" %>
3 <html><head><title>Cluster App Test</title></head>
4 <body>
5 Server Info:
6 <%
7 out.println(request.getLocalAddr() + ": " +request.getLocalPort()+"<br>");%>
8 <%
9 out.println("<br> ID " +session.getId()+"<br>");
10   //如果有新的 Session 属性设置
11   StringdataName =request.getParameter("dataName");
12   if(dataName != null &&dataName.length() > 0) {
13      StringdataValue =request.getParameter("dataValue");
14 session.setAttribute(dataName, dataValue);
15 }
16 out.println("<b>Session 列表</b><br>");
17 System.out.println("============================");
18 Enumeration e =session.getAttributeNames();
19   while(e.hasMoreElements()) {
20      Stringname =(String)e.nextElement();
21      Stringvalue =session.getAttribute(name).toString();
22 out.println( name + "= " +value+"<br>");
23 System.out.println( name + "= " +value);
24 }
25 %>
26   <form action="index.jsp"method="POST">
27     名称:<input type=text size=20 name="dataName">
28      <br>
29     值:<input type=text size=20 name="dataValue">
30      <br>
31     <input type=submit>
32    </form>
33 </body>
34 </html>

到了这里我有两个疑问没有解决,

1.使用Cluster虽然Session复制成功了,但是在网页上使用iframe 框架时候会出现失效

2.文件上传的问题

免责声明:文章转载自《Nginx+Tomcat 集群部署》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇P2P通信标准协议(一)之STUNvirtiofsd下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决 "OperationalError: (sqlite3.OperationalError) no such table: ..."问题

参考:flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table 在用Flask写一个简单的py文件,做一个表单时,发现在login界面登陆的时候,出现: OperationalError: (sqlite3.OperationalError) no such...

Mac下搭建php开发环境【转】

Mac OS X 内置了Apache 和 PHP,这样使用起来非常方便。本文以Mac OS X 10.6.3为例。主要内容包括: 启动Apache 运行PHP 安装MySQL 使用phpMyAdmin 配置PHP的MCrypt扩展库 设置虚拟主机 启动Apache   有两种方法: 打开“系统设置偏好(System Preferences)” ->...

PHP实现IP访问限制及提交次数的方法详解

一、原理 提交次数是肯定要往数据库里写次数这个数据的,比如用户登陆,当用户出错时就忘数据库写入出错次数1,并且出错时间,再出错写2,当满比如5次时提示不允许再登陆,请明天再试,然后用DateDiff计算出错时和now()的时间,如果大于24就再开放让他试。 封IP的话特别是给IP断就比较简单了, 先说给IP段开放的情况:先取出客户访问的IP,为了解释方便,...

ORACLE 锁机制

Refer to:http://blog.csdn.net/tianlesoftware/article/details/4696896 万能解决之道,就是从系统级kill掉lock的process  在数据库中有两种基本的锁类型:排它锁(Exclusive Locks,即X锁)和共享锁(Share Locks,即S锁)。当数据对象被加上排它锁时,其他的事...

ASP.NET 全局变量和页面间传值方法

http://www.cnblogs.com/dgjack/archive/2011/05/28/2060913.html 1. 使用QueryString变量 QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。 如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不...

Hadoop 管理监控工具:Apache Ambari

Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的供应、管理和监控。Ambari目前已支持大多数Hadoop组件,包括HDFS、MapReduce、Hive、Pig、 Hbase、Zookeper、Sqoop和Hcatalog等。 Apache Ambari 支持HDFS、MapReduce、Hive、Pig、Hbase...