Apache&Tomcat&Redis&Nginx配置集锦

‍‍‍‍1,单ip多域名訪問不同網站,並且限制用戶使用IP訪問方法;

Apche httpd 在conf.d或者conf 文件夹下建立新的配置档案

vim zzgds.com.conf

Listen 80
ServerName 10.207.238.65
NameVirtualHost *:80
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/ip
    ServerName 10.207.238.65
    ErrorLog /var/log/httpd/10..zzgds.com-error_log
    CustomLog /var/log/httpd/10.zzgds.com-access_log common
 <Directory "/var/www/html/ip">
     Options Indexes FollowSymLinks
     allow from all 
 </Directory>
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/a1
    ServerName a1.zzgds.com
    ErrorLog /var/log/httpd/a1.zzgds.com-error_log
    CustomLog /var/log/httpd/a2.zzgds.com-access_log common
 <Directory "/var/www/html/a1">
     Options Indexes FollowSymLinks
     allow from all 
 </Directory>
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/a2
    ServerName a2.zzgds.com
    ErrorLog /var/log/httpd/a2.zzgds.com-error_log
    CustomLog /var/log/httpd/a2.zzgds.com-access_log common
 <Directory "/var/www/html/a2">
     Options Indexes FollowSymLinks
     allow from all 
 </Directory>
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName a3.zzgds.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(a3.)?zzgds.com$ [NC]
    RewriteRule ^(/)?$ awis/index.jsp [L]
    JkMount /* awis
    JkMount /awis/* awis
    DirectoryIndex index.jsp
#    ProxyPreserveHost On
#    ProxyPass / http://10.195.168.92:8080/etl/
#    ProxyPassReverse / http://10.195.168.92:8080/etl/
    ErrorLog /var/log/httpd/a3.zzgds.com-error_log
    CustomLog /var/log/httpd/a3.zzgds.com-access_log common
</VirtualHost>

 nginx 实现以上的功能:

user  nobody;
worker_processes  8;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
     server {
            listen 80 default_server;
            server_name localhost;
            location /{
                root   html;
                index  index.html index.htm;
                }
            location /ngx_status{
                stub_status on;
                access_log  off;
                }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
                }
        }
     server {
       listen       80;
       server_name  a1.zzgds.com;
       location / {
            proxy_pass     http://10.207.238.66:8080; 
            proxy_set_header  X-Real-IP  $remote_addr;  
            client_max_body_size  100m;
            client_body_buffer_size     128k;
            proxy_connect_timeout       90;
            proxy_send_timeout          90;
            proxy_read_timeout          90;
            proxy_buffer_size           8k;
            proxy_buffers               4 32k;
            proxy_busy_buffers_size     64k;
            proxy_temp_file_write_size  64k;
            }
    }
    server {
        listen       80;
        server_name  a2.zzgds.com;
        location / {
             proxy_pass     http://10.207.238.67:8080; 
             proxy_set_header  X-Real-IP  $remote_addr;  
             client_max_body_size  100m;
             client_body_buffer_size     128k;
             proxy_connect_timeout       90;
             proxy_send_timeout          90;
             proxy_read_timeout          90;
             proxy_buffer_size           8k;
             proxy_buffers               4 32k;
             proxy_busy_buffers_size     64k;
             proxy_temp_file_write_size  64k;
            }
    }
server {
        listen       80;
        server_name  a3.zzgds.com;
        location / {
             proxy_pass     http://10.195.225.122:8080; 
             proxy_set_header  X-Real-IP  $remote_addr;  
             client_max_body_size  100m;
             client_body_buffer_size     128k;
             proxy_connect_timeout       90;
             proxy_send_timeout          90;
             proxy_read_timeout          90;
             proxy_buffer_size           8k;
             proxy_buffers               4 32k;
             proxy_busy_buffers_size     64k;
             proxy_temp_file_write_size  64k;
            }
    }
}

2,rewrite 重寫示例

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 10

 3,mod_jk轉發Tomcat配置

 a.新建mod_jk.conf文件

vim conf.d/mod_jk.conf

LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.x.so
JkWorkersFile conf.d/workers.properties
JkLogFile ""logs/mod_jk.log"
JkMount /* controller

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名

4,将下载的JK插件mod_jk-1.2.31-httpd-2.2.x.so复制到Apache安装目录的modules目录下。

5,新建并编辑workers.properties文件,内容如下

#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=11009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=12009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller LB========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1

worker.tomcat1.lbfactor = 1 表示tomcat权重,越大表示分的请求越多;

这里可以配置任意多个Tomcat,此处配置了3Tomat服务器,2个本地,1个远程,所以为了它们都能够顺利启动起来,本地的服务器端口都是不同的,如果Tomcat不再同一机器上,没必要改端口的。

tomcat实现访问不加项目名访问需要添加

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="web" reloadable="true" />


 6,Tomcat redis session 共享

redis安装测试环境为:

java version "1.7.0_79"

apache-tomcat-7.0.67

将这三个文件复制到tomcat lib目录下

commons-pool-1.6.jar

tomcat-redis-session-manager-1.2-tomcat-7.jar

jedis-2.1.0.jar

 vim conf/context.xml 增加以下部分内容

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
         <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
         host="10.207.238.60"
         port="6379"
         database="0"
         maxInactiveInterval="60"
         />

这样就可以使用redis,但是在后续的测试中需要在项目中添加,在web 建立WEB-INF文件夹 建立web.xml文件 添加

 <distributable/>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
 
  <display-name>Welcome to Tomcat</display-name>
  <distributable/>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

7,另外一种方式实现session共享 通过cluster

vim conf/server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

注意当使用redis测试session共享时 最好不要设置jvmRoute 因为设置会导致redis 内存使用超过100%,暂时不知什么原因

 vim conf/server.xml 添加一下集群配置部分

   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                channelSendOptions="8">
 
        <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="4000" 
                        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>

8,测试session共享之办法

1,在webapps下建立部署目录web 建立相应的主页index.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
  out.println("<br> ID " + session.getId()+"<br>");
  // 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
 if (dataName != null && dataName.length() > 0) {
  String dataValue = request.getParameter("dataValue");
  session.setAttribute(dataName, dataValue);
}
out.print("<b>Session Cloumn</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
  String name = (String)e.nextElement();
  String value = session.getAttribute(name).toString();
  out.println( name + " = " + value+"<br>");
     System.out.println( name + " = " + value);
  }
  %>
   <form. action="index.jsp" method="POST">
    Name:<input type=text size=20 name="dataName">
    <br>
    Value:<input type=text size=20 name="dataValue">
    <br>
    <input type=submit>
    </form>
</body>
</html>

在web 建立WEB-INF文件夹 建立web.xml文件 添加

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
 
  <display-name>Welcome to Tomcat</display-name>
  <distributable/>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

或者使用另外一种jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Apache+tomcat负载均衡+redis会话管理</title>
</head>
<body>
<h1><font color="blue">集群节点1</font></h1>
    <table border="1">
      <tr>
        <td>Session ID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
 <p>
</body>
</html>

分别设置集群节点1和节点2 测试;

9,Nginx+tomcat session 

A,nginx 工作流程

Apache&Tomcat&Redis&Nginx配置集锦_第1张图片

B,下载相应版本的nginx 以及nginx sticky

此环境为:

nginx-1.4.7.tar.gz

nginx-sticky-module-1.1.tar.gz

yum -y install pcre-devel openssl-devel

解压相应的软件包

安装nginx

./configure --prefix=/usr/local/nginx-1.4.7 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=../nginx-sticky-module-1.1
make
make install

C,nginx 的upstream使用sticky,如下

整个配置文件如下:

user  nobody;
worker_processes  8;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    gzip  on;
    upstream tomcat {
#   sticky;
    server 10.207.238.66:8080;
    server 10.207.238.67:8080;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass     http://tomcat; 
            proxy_set_header  X-Real-IP  $remote_addr;  
            client_max_body_size  100m;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

D,测试nginx sticky

测试还是依照上面方法,进行测试当不使用sticky参数是SESSION id是会频繁变动的,当使用参数时 一直就保持当前的session id 

具体的过程不在重复;

E,nginx sticky其他语法

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback];
name: 可以为任何的string字符,默认是route
domain:哪些域名下可以使用这个cookie
path:哪些路径对启用sticky,例如path/test,那么只有test这个目录才会使用sticky做负载均衡
expires:cookie过期时间,默认浏览器关闭就过期,也就是会话方式。
no_fallbackup:如果设置了这个,cookie对应的服务器宕机了,那么将会返回502(bad gateway 或者 proxy error),建议不启用

7、nginx sticky expires用法

upstream tomcat {
    sticky expires=1h;
    server 10.207.238.66:8080;
    server 10.207.238.67:8080;
    }

启用了过期,cookie如下截图,cookie 1个小时才过期;




















你可能感兴趣的:(redis,tomcat,nginx,session共享,Sticky)