Apache&Tomcat&Redis&Nginx配置集锦

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

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

vim zzgds.com.confnginx

Listen 80
ServerName 10.207.238.65
NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin jason.zr.zhang@icloud.com
    DocumentRoot /var/www/html
    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">
     Options Indexes FollowSymLinks
     allow from all  
 </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin jason.zr.zhang@icloud.com
    ServerName scm-sz.zzgds.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^scm-sz\.zzgds\.com$ [NC]
    RewriteRule ^/?$ /%{HTTP_HOST}
    RewriteRule ^/scm-sz.zzgds.com/?$ /imes/ [R]
    JkMount /* imes
    JkMount /imes/* imes
    ErrorLog /var/log/httpd/imes.zzgds.com-error_log
    CustomLog /var/log/httpd/imes.zzgds.com-access_log common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin jason.zr.zhang@icloud.com
    ServerName rfid-sz.zzgds.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^rfid-sz\.zzgds\.com$ [NC]
    RewriteRule ^/?$ /%{HTTP_HOST}
    RewriteRule ^/rfid-sz.zzgds.com/?$ /whsys/ [R]
    JkMount /* whsys
    JkMount /whsys/* whsys
    ErrorLog /var/log/httpd/whsys.zzgds.com-error_log
    CustomLog /var/log/httpd/whsys.zzgds.com-access_log common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin jason.zr.zhang@icloud.com
    ServerName csm-sz.zzgds.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^csm-sz\.zzgds\.com$ [NC]
    RewriteRule ^/?$ /%{HTTP_HOST}
    RewriteRule ^/csm-sz.zzgds.com/?$ /csmsys/ [R]
    JkMount /* csmsys
    JkMount /csmsys/* csmsys
    ErrorLog /var/log/httpd/csmsys.zzgds.com-error_log
    CustomLog /var/log/httpd/csmsys.zzgds.com-access_log common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin jason.zr.zhang@icloud.com
    ServerName ews-sz.zzgds.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ews-sz\.zzgds\.com$ [NC]
    RewriteRule ^/?$ /%{HTTP_HOST}
    RewriteRule ^/ews-sz.zzgds.com/?$ /samplesys/ [R]
    JkMount /* samplesys
    JkMount /samplesys/* samplesys
    ErrorLog /var/log/httpd/samplesys.zzgds.com-error_log
    CustomLog /var/log/httpd/samplesys.zzgds.com-access_log common
</VirtualHost>

 nginx 实现以上的功能:web

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;
}

include /usr/local/nginx-1.6.3/conf/tcp_proxy.conf;

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  scm-sz.zzgds.com;
	access_log  logs/access_imes.log  main;
	location / {  
        rewrite ^/(.*)$ /imes/$1 last;  
          }  
     location ~* ^/imes/.*$ {
         proxy_pass     http://10.173.50.210; 
         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  rfid-sz.zzgds.com;
	access_log  logs/access_whsys.log  main;
	location / {  
        rewrite ^/(.*)$ /whsys/$1 last;  
          }  
     location ~* ^/whsys/.*$ {
         proxy_pass     http://10.173.50.210; 
         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  csm-sz.zzgds.com;
	access_log  logs/access_csmsys.log  main;
	location / {  
        rewrite ^/(.*)$ /csmsys/$1 last;  
          }  
     location ~* ^/csmsys/.*$ {
         proxy_pass     http://10.173.50.210; 
         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  ews-sz.zzgds.com;
	access_log  logs/access_samplesys.log  main;
	location / {  
        rewrite ^/(.*)$ /samplesys/$1 last;  
          }  
     location ~* ^/samplesys/.*$ {
         proxy_pass     http://10.173.50.210; 
         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;
            }
    	}
}

另一种方式redis

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;
}
include /usr/local/nginx-1.6.3/conf/tcp_proxy.conf;
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  scm-sz.zzgds.com rfid-sz.zzgds.com csm-sz.zzgds.com ews-sz.zzgds.com;
    if ($host = "scm-sz.zzgds.com"){
             rewrite ^/(.*)$ /imes/$1 last;
          }
    if ($host = "rfid-sz.zzgds.com"){
             rewrite ^/(.*)$ /whsys/$1 last;
          }
    if ($host = "csm-sz.zzgds.com"){
             rewrite ^/(.*)$ /csmsys/$1 last;
          }
    if ($host = "ews-sz.zzgds.com"){
             rewrite ^/(.*)$ /samplesys/$1 last;
          }
  
    location ~* ^/cfm/.*$ {
             proxy_pass     http://10.195.225.123;
             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;
            }
    location ~* ^/cfm/.*$ {
             proxy_pass     http://10.195.225.123;
             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;
            }
    location ~* ^/cfm/.*$ {
             proxy_pass     http://10.195.225.123;
             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;
            }
    location ~* ^/cfm/.*$ {
             proxy_pass     http://10.195.225.123;
             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 重寫示例
shell

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配置express

 a.新建mod_jk.conf文件apache

vim conf.d/mod_jk.confvim

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 工做流程

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),建议不启用

七、nginx sticky expires用法

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

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

相关文章
相关标签/搜索