首页 > 代码库 > tomct的nginx反带和httpd的负载均横
tomct的nginx反带和httpd的负载均横
Nginx的安装
tar -zxvf pcre-8.30.tar.gz
cd pcre-8.30
ntpdate time.windows.com
./configure
make
make install
groupadd www
useradd www -g www
yum install -y openssl-devel
tar -zxvf nginx-1.2.0.tar.gz
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make
make install
NGINX 配置文件
user www www;
worker_processes 8;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
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 16;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 80;
server_name 192.168.88.145;
root /usr/local/nginx/html;
index index.html index.htm index.php index.jsp index.do;
location ~ \.(jsp|jspdx|do)?$ { #所有的jsp页面转交由tomcat处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8080; #转向tomcat处理
}
Location~ .*\.(htm|html|gif|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { #设置静态网页直接由nginx进行处理
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
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 /data/logs/access.log access;
}
}
与tomcat整合:
软件包:
jdk-7-linux-i586-rpm.bin
apr-util-1.3.12.tar.gz
apache-tomcat-7.0.27.tar.gz
apr-1.4.5.tar.gz
tar –zxvf apr-1.4.5.tar.gz
cd apr-1.4.5
./configur
Make
Make install
Tar –zxvf apr-util-1.3.12.tar.gz
./configure --with-apr=/usr/local/apr
Make
Make install
./ jdk-7-linux-i586-rpm.bin
Rpm –ivh jdk-7-linux-i586-rpm
Tar –zxvf apache-tomcat-7.0.27.tar.gz
cp –a apache-tomcat-7.0.27 /usr/local/tomcat
vi /etc/profile
在最后加:
export JAVA_HOME="/usr/java/jdk1.7.0/"
export PATH="$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin"
export CLASSPATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
export CATALINA_HOME="/usr/local/tomcat"
保存退出
export /etc/profile
Tomcat配置文件
找到下面内容,并改成
<Host name="192.168.88.145" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/nginx/html/" />
然后直接访问:http://192.168.88.145/jsp.jsp,就可以看到jsp网页了
httpd负载均衡
准备:
1:Apache,下载后直接安装,安装完成后能够访问本地http://localhost/,代表安装完成。此次使用Apache2.2版本。
2:Tomcat,下载后直接安装,安装完成后能够访问本地http://localhost:8080,代表安装完成。此次使用Tomcat6.0版本。
3:JK,Apache/IIS 用来连接后台Tomcat的模块,支持集群和负载均衡。此次使用mod_jk-1.2.31-httpd-2.2.3.so版本。
方法一:-----------------------------------------------------
配置Apache:
1:修改httpd.conf
我的安装路径:E:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
在最后加上Include conf/mod_jk.conf
2:新建mod_jk.conf
在conf目录下新建mod_jk.conf配置文件,内容如下:
[plain] view plaincopy
# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
# Where to find workers.properties
# Update this path to match your conf directory location (put workers.properties next to httpd.conf)
JkWorkersFile conf/workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile logs/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# Send everything for context /examples to worker named worker1 (ajp13)
JkMount /* controller
3:新建workers.properties配置文件,内容如下:
[plain] view plaincopy
#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=172.23.49.131
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
# Define preferred failover node for tomcat1
#worker.tomcat1.redirect=tomcat2##如果tomcat1宕机,则把请求叫给tomcat2处理
#========tomcat2========
worker.tomcat2.port=9009
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
# Disable tomcat2for all requests except failover
#worker.tomcat2.activation=disabled ##当tomcat1正常工作时,tomcat2”休息“
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
4:添加文件
将刚才下载的mod_jk-1.2.31-httpd-2.2.3.so复制到E:\Program Files\Apache Software Foundation\Apache2.2\modules目录下。
到这里Apache的配置基本完成————————————————————————————————————————
配置Tomcat:
1:将刚才下载的tomcat复制两份apache-tomcat-01,apache-tomcat-02。
假如2个tomcat在同一机器上,我们需要修改部分端口。打开apache-tomcat-02的conf目录下的server.xml。
2:修改tomcat02的shutDown端口为9005,tomcat01保持8005不变。
3:修改tomcat02的Connector端口为9080,tomcat01保持8080不变。
4:修改AJP/1,3的端口为9009,tomcat01保持8009不变。
5:修改<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">标签内容,加上jvmRoute="tomcat1"属性,注:tomcat01和tomcat02都要修改。
6:启用<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>标签,注:tomcat01和tomcat02都要启用。
到这里Tomcat的配置基本完成————————————————————————————————————————
方法二:-----------------------------------------------------
配置Apache:
1:修改httpd.conf
我的安装路径:E:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
在最后加上:
[plain] view plaincopy
ProxyRequests Off
ProxyPass / balancer://abc/ stickysession=jsessionid nofailover=On
<Proxy balancer://abc/>
BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat1
BalancerMember ajp://localhost:9009 loadfactor=1 route=tomcat2
</Proxy>
配置Tomcat:
同方法一
到这里配置基本完成————————————————————————————————————————
测试成果:
在tomcat01和tomcat02的webapps中添分别加文件夹test,在test下添加文件夹WEB-INF和test.jsp,在WEB-INF中添加web.xml配置文件。
web.xml配置文件内容如下:
[plain] view plaincopy
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
<distributable />
</web-app>
test.jsp配置如下:
[plain] view plaincopy
<%@ 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>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = http://www.mamicode.com/request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = http://www.mamicode.com/session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
到这里测试内容配置基本完成
访问地址http://127.0.0.1/test/test.jsp,显示页面
F5刷新页面后
多次刷新页面,其中sessionID为同一个id,说明session已经实现复制。
输入内容查看
F5刷新页面
可以发现复制session的同时,session中的变量也同时被复制。
本文出自 “hanye” 博客,请务必保留此出处http://9025736.blog.51cto.com/9015736/1590143
tomct的nginx反带和httpd的负载均横