1. 企业实战Nginx+Tomcat动静分离架构 Nginx+tomcat是目前主流的java web架构,Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat、Resin出来动态页面。 动静分离从目前实现角度来讲大致分为两种,一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过nginx来分开。这样也是本次课程要讲解的,具体怎么来实现呢,如下图,通过location指定不同的后缀名实现不同的请求转发。 通过expires参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体Expires定义:是给一个资源设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。 此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用Expires来缓存),我这里设置3d,表示在这3天之内访问这个URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,如果有修改,则直接从服务器重新下载,返回状态码200。 搭建环境:两台虚拟机 nginx服务器 1
2
3
4
5
| [iyunv@192_168_77_189 ~]# uname -a
Linux 192_168_77_189 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[iyunv@192_168_77_189 ~]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
|
tomcat服务器
1
2
3
4
5
| [iyunv@192_168_77_188 ~]# uname -a
Linux 192_168_77_188 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[iyunv@192_168_77_188 ~]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
|
NGINX安装(NGINX服务器)
1
2
3
4
5
6
| yum install pcre-devel pcre -y
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -xzvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
|
安装完后用/usr/local/nginx/sbin/nginx 启动服务然后访问可以看到测试页面
JDK,tomcat安装(tomcat服务器)
1
2
3
| tar -xzvf apache-tomcat-7.0.63.tar.gz
cp apache-tomcat-7.0.63 /usr/local/tomcat1 -a
cp apache-tomcat-7.0.63 /usr/local/tomcat2 -a
|
TOMCAT配置文件server.xml分别修改tomcat端口:
shutdown 端口:8005 主要负责启动关闭.
ajp端口:8009 主要负责通过ajp均衡(常用于apache和tomcat整合)
http端口:8080 可以通过web页面直接访问(nginx+tomcata整合)
tomcat1:默认端口
tomcat2:分别是8006 8010 8081
启动tomcat
1
2
3
4
5
| /usr/local/tomcat1/bin/catalina.sh start
/usr/local/tomcat2/bin/catalina.sh start
ps -ef |grep tomcat可以查看服务以及启动
netstat -ntulp | grep java 可以查看服务启动的端口
|
客户端访问:
http://192.168.77.188:8080/
http://192.168.77.188:8081/
可以看到tomcat的测试页面
如果需要修改tomcat发布目录为自己制定的目录,需要做如下调整,创建两个发布目录:
1
| mkdir -p /data/webapps/{www1,www2}
|
编辑vi /usr/local/tomcat1/conf/server.xml 在最后</Host>前一行加下内容
1
| <Context path="" docBase="/data/webapps/www1" reloadable="false"/>
|
编辑vi /usr/local/tomcat2/conf/server.xml 在最后</Host>前一行加下内容
1
| <Context path="" docBase="/data/webapps/www2" reloadable="false"/>
|
tomcat1发布目录内容:index.jsp
1
2
3
4
5
6
| <html>
<body>
<h1>TOMCAT_1 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>
|
tomcat2发布目录内容:index.jsp
1
2
3
4
5
6
| <html>
<body>
<h1>TOMCAT_2 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>
|
然后重启服务通过IP加端口访问测试,能正常访问
Nginx+tomcat整合:
Nginx动静分离均衡配置:
nginx服务器上配置nginx.conf文件
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
| worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_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 application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
#均衡模块
upstream web_app {
#防止访问登录时候切换,我这里做测试将他注释
# ip_hash;
server 192.168.77.188:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.77.188:8081 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost www.lijq.com;
index index.jsp index.html index.htm;
#本地发布目录/data/www
root /data/www;
#所有请求从根走
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_app;
expires 3d;
}
#请求动静分离优先选择
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_app;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /data/www;
#请求浏览器缓存时间3天
expires 3d;
}
}
}
|
测试nginx配置文件
[iyunv@192_168_77_189 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
平滑重启
/usr/local/nginx/sbin/nginx -s reload
测试通过http://192.168.77.189/访问可以查看均衡成功,动静分离测试也成功
|