Debian 6下配置Nginx+Tomcat负载均衡(支持Session共享)
<>一、配置环境。
<>1、><>²><>²><>²><>²><>²><>2、><>²><>²><>²><>²><>²><>二、在/>/>/>/>/>/>>>>>>>>>>>>>Srv76/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>上安装Nginx。
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>/>/>/>/>/>/>/>/>/>/>/>/>
>>>>>>>>>>>> <>1、使用如下命令安装编译Nginx所需软件包。
srv76:~# apt-get upgrade
srv76:~# apt-get install zlibc libpcre++-dev bzip2 autoconf automake g++ gcc /
make libssl-dev libpcre3-dev build-essential
<>2、使用如下命令下载并编译安装Nginx及nginx_upstream_jvm_route。
srv76:~# adduser --system --no-create-home --disabled-login /
--disabled-password --group nginx
srv76:~# cd /usr/src
srv76:src# wget http://nginx.org/download/nginx-1.0.4.tar.gz
srv76:src# svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ nginx-upstream-jvm-route-read-only
srv76:src# tar -xvzf nginx-1.0.4.tar.gz
srv76:src# cd nginx-1.0.4
srv76:nginx-1.0.4# patch -p0 < ../nginx-upstream-jvm-route-read-only/jvm_route.patch
srv76:nginx-1.0.4# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx /
--with-http_stub_status_module --with-http_ssl_module /
--with-pcre --add-module=../nginx-upstream-jvm-route-read-only
srv76:nginx-1.0.4# make && make install
<>3、使用如下命令配置Nginx服务启动命令。
srv76:~# chmod +x /etc/init.d/nginx
srv76:~# /usr/sbin/update-rc.d -f nginx defaults
srv76:~# ln -s /usr/local/nginx/sbin/* /sbin/
srv76:~# mkdir /var/log/nginx
srv76:~# chgrp nginx /var/log/nginx/
srv76:~# chmod g+w /var/log/nginx/
<>三、安装及配置Tomcat及Java。
<>1、在Srv76、Srv77上使用如下命令安装Tomcat及Java。
srv76:~# apt-get install libmysql-java tomcat6 libservlet2.5-java libtomcat6-java /
mysql-server sun-java6-jdk sun-java6-bin tomcat6-admin /
sun-java6-jre
<>2、在Srv76将/etc/tomcat6/server.xml文件中“<Engine name="Catalina" defaultHost="localhost">”修改为如下内容。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="srv76">
<>3、在Srv77将/etc/tomcat6/server.xml文件中“<Engine name="Catalina" defaultHost="localhost">”修改为如下内容。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="srv77">
<>4、在Srv76、Srv77上使用如下命令重新启动Tomcat。
srv76:~# /etc/init.d/tomcat6 restart
<>四、修改/usr/local/nginx/conf/nginx.conf文件如下内容。
user nginx nginx;
worker_processes 30;
error_log /var/log/nginx/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 65535;
}
http
{
upstream backend {
server 192.168.159.76:8080 srun_id=srv76;
server 192.168.159.77:8080 srun_id=srv77;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}
include mime.types;
default_type application/octet-stream;
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;
server_tokens off;
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.1;
gzip_comp_level 2;
gzip_types text/plain text/css application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]/.(?!.*SV1)";
server
{
listen 80;
server_name 192.168.159.76;
index index.html index.htm index.jsp;
root /var/www;
location /
{
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 /var/log/nginx/jsp.access.log;
}
}
本文出自 “相濡以沫” 博客
/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>/>