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,此處配置了3個Tomat服務器,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個小時才過時;