nginx_upstream_jvm_route 实验

基础知识:


这个模块通过session cookie的方式来获取session粘性。如果在cookie和url中并没有session,则这只是个简单的round-robin 负载均衡。

jvm_route的原理(from 作者 Weibin Yao):

1. 一开始请求过来,没有带session信息,jvm_route就根据round robin的方法,发到一台tomcat上面。

2. tomcat添加上session 信息,并返回给客户。

3. 用户再此请求,jvm_route看到session中有后端服务器的名称,它就把请求转到对应的服务器上。



暂时jvm_route模块还不支持默认fair的模式。jvm_route的工作模式和fair是冲突的。对于某个特定用户,当一直为他服务的tomcat宕机后,默认情况下它会重试max_fails的次数,如果还是失败,就重新启用round robin的方式,而这种情况下就会导致用户的session丢失。

总的说来,jvm_route是通过session_cookie这种方式来实现session粘性,将特定会话附属到特定tomcat上,从而解决session不同步问题,但无法解决宕机后会话转移问题。


Jvm_route实验部分:

准备工作:

1.下载apache-tomcat,分别安装到~/apache-tomcat00,~/apache-tomcat01目录下。

2.下载nginx-0.7.65

3.下载nginx-upstream-jvm-route

wget http://nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.1.tar.gz

安装:

进入到nginx-0.7.65安装目录:

#patch –p0 < /path/to/jvm_route.patch

编译安装nginx

#./configure --prefix=/path/to/install/  --with-http_stub_status_module --with-http_ssl_module –add-module=/path/to/jvm_route/

#make

#make install

修改配置:

1. 修改apache-tomcat00/conf/server.xml配置:


   修改端口号,避免冲突:




2. 参照1,修改apache-tomcat01

3.分别启动apache-tomcat00,apache-tomcat01

4. nginx配置如下:
 
   

user liuxiaoqin liuxiaoqin;

worker_processes 2;

#error_log logs/error.log;

#error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events {

use epoll;

worker_connections 1024;

}

http {

upstream newtomcat {

#ip_hash;

server 127.0.0.1:8080 max_fails=3 fail_timeout=30s srun_id=a;

server 127.0.0.1:8081 max_fails=3 fail_timeout=30s srun_id=b;

jvm_route $cookie_JSESSIONID|sessionid reverse;

}

include mime.types;

default_type application/octet-stream;

log_format access '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

#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 access;

sendfile on;

tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

location /aa/{

proxy_pass http://newtomcat;

proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header Host $http_host;

}

location /bb/{

proxy_pass http://newtomcat;

proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header Host $http_host;

}

location /Nginxstatus {

stub_status on;

access_log off;

}

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

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen 443;

# server_name localhost;

# ssl on;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;

# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}
5.在apache-tomcat00目录/webapps/,

mkdir aa;

创建文件index.jsp.内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
   
   
   
     a
   
<%out.print(request.getSession()) ;%>
    <%out.println(request.getHeader("Cookie")); %>
   

6.在apache-tomcat01目录下/webapps/,

mkdir bb;

创建文件index.jsp.内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
   
   
   
     b
   
<%out.print(request.getSession()) ;%>
    <%out.println(request.getHeader("Cookie")); %>
   


这样,我们在浏览器中输入127.0.0.1/aa/就可以看到输出, 并且可以观察到无论刷新多少次,输出结果不变。

你可能感兴趣的:(linux)