为什么80%的码农都做不了架构师?>>>
1,单ip多域名訪問不同網站,並且限制用戶使用IP訪問方法;
Apche httpd 在conf.d或者conf 文件夹下建立新的配置档案
vim zzgds.com.conf
Listen 80
ServerName 10.207.238.65
NameVirtualHost *:80
ServerAdmin [email protected]
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
Options Indexes FollowSymLinks
allow from all
ServerAdmin [email protected]
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
ServerAdmin [email protected]
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
ServerAdmin [email protected]
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
ServerAdmin [email protected]
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
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;
}
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;
}
}
}
另外一种方式
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 重寫示例
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,此处配置了3个Tomat服务器,2个本地,1个远程,所以为了它们都能够顺利启动起来,本地的服务器端口都是不同的,如果Tomcat不再同一机器上,没必要改端口的。
tomcat实现访问不加项目名访问需要添加
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 增加以下部分内容
这样就可以使用redis,但是在后续的测试中需要在项目中添加,在web 建立WEB-INF文件夹 建立web.xml文件 添加
Welcome to Tomcat
Welcome to Tomcat
7,另外一种方式实现session共享 通过cluster
vim conf/server.xml
注意当使用redis测试session共享时 最好不要设置jvmRoute 因为设置会导致redis 内存使用超过100%,暂时不知什么原因
vim conf/server.xml 添加一下集群配置部分
8,测试session共享之办法
1,在webapps下建立部署目录web 建立相应的主页index.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster App Test
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
out.println("
ID " + session.getId()+"
");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("Session Cloumn");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"
");
System.out.println( name + " = " + value);
}
%>
Name:
Value:
在web 建立WEB-INF文件夹 建立web.xml文件 添加
Welcome to Tomcat
Welcome to Tomcat
或者使用另外一种jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Apache+tomcat负载均衡+redis会话管理
集群节点1
Session ID
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
分别设置集群节点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),建议不启用
7、nginx sticky expires用法
upstream tomcat {
sticky expires=1h;
server 10.207.238.66:8080;
server 10.207.238.67:8080;
}
启用了过期,cookie如下截图,cookie 1个小时才过期;