nginx+多个tomcat的负载均衡
转自:http://www.open-open.com/lib/view/open1394854254103.htmltomcat处理文件扩展名为.jsp,.do的请求
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
网络架构
nginx主机:10.10.54.87
tomcat主机1:10.10.54.87
tomcat主机2:10.10.54.87
(1)安装nginx-1.4.5
# tar xvf nginx-1.4.5.tar.gz
# cd nginx-1.4.5
# ./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
# make && make install
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
--启动nginx
(2)配置DNS
# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "dogs.com" IN {
type master;
file "named.dogs.com";
};
# vim /var/named/named.dogs.com
$TTL 600
@ IN SOA dogs.com. root (2014030401 1H 15M 1W 1D);
@ IN NS dogs.com.
dogs.com. IN A 10.10.54.87
www.dogs.com. IN A 10.10.54.87
bbs.dogs.com. IN A 10.10.54.87
hr.dogs.com. IN A 10.10.54.87
(3)安装JDK
# rpm -ivh jdk-7u51-linux-x64.rpm
# rpm -ql jdk-1.7.0_51-fcs.x86_64
--查看JDK的安装路径
/usr/java/jdk1.7.0_51
(4)安装配置tomcat
# tar xvf apache-tomcat-7.0.52.tar.gz
# mv apache-tomcat-7.0.52 /usr/local/tomcat
//给tomcat添加JAVA主目录,在96行添加
# vim /usr/local/tomcat/bin/catalina.sh
JAVA_HOME=/usr/java/jdk1.7.0_51
CATALINA_HOME=/usr/local/tomcat
//做启动脚本
# cd /usr/local/tomcat/bin/
# cp catalina.sh /etc/init.d/tomcat
# vim /etc/init.d/tomcat
#chkconfig: 2375 74 76
#description:tomcat start stop
# chmod +x /etc/init.d/tomcat
(5)第二台tomcat
# tar xvf apache-tomcat-7.0.52.tar.gz
# mv apache-tomcat-7.0.52 /usr/local/tomcat2
//给tomcat添加JAVA主目录,在96行添加
# vim /usr/local/tomcat2/bin/catalina.sh
JAVA_HOME=/usr/java/jdk1.7.0_51
CATALINA_HOME=/usr/local/tomcat2
//做启动脚本
# cd /usr/local/tomcat2/bin/
# cp catalina.sh /etc/init.d/tomcat2
# vim /etc/init.d/tomcat2
#chkconfig: 2355 73 75
#description:tomcat start stop
# chmod +x /etc/init.d/tomcat
//更改配置
# cd /usr/local/tomcat2
//更改三个端口
# vim conf/server.xml
##将8005改为其他,如:9005
<Server port="8005" shutdown="SHUTDOWN">
##将8080改为其他,如:9090
<Connector port="8080" protocol="HTTP/1.1"
##将8009改为其他,如:9009
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
# mkdir -p /var/www/
# cd /var/www/
# mkdir ROOT --注意必须是ROOT
# cd ROOT/
# echo "Hello10.10.54.87" >index.jsp
# vim /usr/local/tomcat2/conf/server.xml
<Host name="localhost" appBase="/var/www"
--appBase地址更改
(6)更改nginx配置
# vim /usr/local/nginx/conf/nginx.conf
user apache apache;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
--添加下面内容
upstream www.dogs.com {
server 10.10.54.87:8080 weight=1;
server 10.10.54.87:9090 weight=1;
}
server {
listen 80;
server_name www.dogs.com;
root /var/www/html;
index index.html index.htm index.jsp;
location / {
proxy_pass http://www.dogs.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*.jsp$ {
index index.jsp;
proxy_pass http://localhost:8080;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
(7)测试
在浏览器中输入“http://www.dogs.com/”
刷新页面会有变化
页:
[1]