nginx_upstream_jvm_route安裝配置

測試環境:
server1 服務器上安裝了 nginx + tomcat01
server2 服務器上只安裝了 tomcat02
server1 IP 地址: 192.168.2.88
server2 IP 地址: 192.168.2.89
安裝步驟:
1. 在server1 上安裝配置 nginx + nginx_upstream_jvm_route
shell $> wget -c http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
shell $> svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ nginx-upstream-jvm-route-read-only
shell $> tar zxvf nginx-0.7.61
shell $> cd nginx-0.7.61
shell $> patch -p0 < ../nginx-upstream-jvm-route-read-only/jvm_route.patch
shell $> useradd www
shell $> ./configure --user=www --group=www --prefix=/usr/local//nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-upstream-jvm-route-read-only
shell $> make
shell $> make install
2.分別在兩臺機器上安裝 tomcat和java (略)
設置tomcat的server.xml, 在兩臺服務器的tomcat的配置文件中分別找到:
<Engine name="Catalina" defaultHost="localhost" >
分別修改爲:
Tomcat01:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="a">
Tomcat02:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="b">
並在webapps下面建立aa文件夾,在裏面建立要測試的index.jsp文件,內容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
%>
<html>
<head>
</head>
<body>
88
<!--server1 這裏爲 88 -->
<br />
<%out.print(request.getSession()) ;%>
<!--輸出session-->
<br />
<%out.println(request.getHeader("Cookie")); %>
<!--輸出Cookie-->
</body>
</html>
兩個tomcat一樣只需要修改紅色的部分
分別啓動兩個tomcat
3.設置nginx
shell $> cd /usr/local/nginx/conf
shell $> mv nginx.conf nginx.bak
shell $> vi nginx.conf
## 以下是配置 ###
user www www;
worker_processes 4;
error_log logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 2048;
}
http
{
upstream backend {
server 192.168.2.88:8080 srun_id=a;
server 192.168.2.89:8080 srun_id=b;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}
include mime.types;
default_type application/octet-stream;
#charset gb2312;
charset UTF-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
limit_rate 1024k;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
#gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name 192.168.2.88;
index index.html index.htm index.jsp;
root /var/www;
#location ~ .*\.jsp$
location / aa/
{
proxy_pass http://backend;
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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location /Nginxstatus {
stub_status on;
access_log off;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
# access_log off;
}
}
4.測試
打開瀏覽器,輸入:http://192.168.2.88/aa/
刷新了N次還都是88,也就是補丁起作用了,cookie 值也獲得了,爲了測試,我又打開了“遨遊瀏覽器”(因爲session 和 cookie問題所以從新打開別的瀏覽器),輸入網址:
http://192.168.2.88/aa/
顯示89,刷新N次後還是89,大家測試的時候如果有疑問可一把 nginx 配置文件的
srun_id=a srun_id=b 去掉,然後在訪問,就會知道頁面是輪詢訪問得了!!
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 8672
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章