配置Nginx反向代理Tomcat,并实现负载均衡、动静分离


前言

Nginx与Apache HTTP Server Project(简称Apache)一样,都是开源的HTTP服务器软件,它通常运行在服务器之上,绑定服务器的IP地址并监听某一个tcp端口来接收并处理HTTP请求,这样客户端(一般来说是IE, Firefox,Chrome这样的浏览器)就能够通过HTTP协议来获取服务器上的网页(HTML格式)、文档(PDF格式)、音频(MP4格式)、视频(MOV格式)等等资源。而Tomcat是用来处理java程序的解释器。本身apache也好,nginx也好,都是无法直接处理java语言的,只能通过设置,当收到java文件请求时,传给后方tomcat处理,再把tomcat的反应回给浏览器。

环境

JDK : 1.8.0_101
Nginx : 9.0.0.M9
Tomcat : 1.10.0
操作系统 : ubuntu-16.04(64)

反向代理

反向代理

什么是反向代理

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

从上图可以看出:反向代理服务器位于网站机房,代理网站Web服务器接收Http请求,对请求进行转发。

反向代理的作用

  • 保护网站安全:任何来自Internet的请求都必须先经过代理服务器;

  • 通过配置缓存功能加速Web请求:可以缓存真实Web服务器上的某些静态资源,减轻真实Web服务器的负载压力;

  • 实现负载均衡:充当负载均衡服务器均衡地分发请求,平衡集群中各个服务器的负载压力;

配置

在配置之前,如果怕自己在配置过程中出错,可以将需要操作的配置文件进行备份,只需要使用sudo cp -rf fileName fileName-copy 即可备份文件。

Tomcat

修改/opt/tomcat9.0/conf目录下的server.xml文件

1
2
3
4
<Host name="localhost" appBase="webapps/springmvc" unpackWARs="true" autoDeploy="true">
<Context path="/springmvc" docBase="springmvc.war"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

Nginx

修改/var/log/nginx目录下的default文件,保证server_name与上面Tomcat配置文件server.xml中的Host name 是一致的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
#监听端口
listen 80 default_server;
listen [::]:80 default_server;
#网站程序根目录,一般Nginx和Tomcat在同一个目录
root /opt/tomcat9.0/webapps/springmvc;
index index.html index.htm index.nginx-debian.html;
#server_name要与Tomcat的server.xml中的Host name一致
server_name localhost;
location / {
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://192.168.182.132:8080;
}
}

Ubuntu下查询本机IP命令 ifconfig -a

测试

确认保存好配置文件之后,启动Tomcat和Nginx服务器,在浏览器中输入http://localhost/springmvc/test,会出现以下页面

成功页面

此时我们可以查看一下Tomcat和Nginx的日志
Nginx(/var/log/nginx/access.log)

1
127.0.0.1 - - [07/Sep/2016:15:30:47 +0800] "GET /springmvc/test HTTP/1.1" 200 101 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0"

Tomcat(/opt/tomcat9.0/logs/localhost_access_log.xxx.txt)

1
192.168.182.132 - - [07/Sep/2016:15:30:47 +0800] "GET /springmvc/test HTTP/1.0" 200 106

负载均衡

负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务

Nginx支持的负载均衡策略

1 round-robin
requests to the application servers are distributed in a round-robin fashion
2 least-connected
next request is assigned to the server with the least number of active connections
3 ip-hash
a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address)

负载均衡的示例

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
upstream tomcat {
server 192.168.182.132:8080 weight=2;
server 192.168.182.132:8081 weight=1;
server 192.168.182.132:8082;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /opt/tomcat9.0/webapps/springmvc;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
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://tomcat;
}
}

动静分离

从应用方面讲,Tomcat一般是做动态分析才会用到,支持jsp的解析,而Nginx一般是做静态分析,本身并不具备动态解析功能,需要配置其他插件或者通过其他软件系统才具备动态功能;而在性能方面,不做系统调优的情况下,Tomcat一般支持并发并不高,而Nginx在静态方面支持并发轻松达几万。为了充分利用两者优秀的性能,我们让Tomcat处理用户请求jsp页面实现动态分离,用Nginx处理css、js、html等静态资源,提高性能。

具体实现

创建静态资源放置的文件夹

将项目中放置静态资源的文件夹拷贝至/opt/springmvc目录下,必须保证各个静态资源的相对路径和在项目中是一致的,否则会出现404错误

server下配置对应的localtion

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
upstream tomcat {
server 192.168.182.132:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /opt/tomcat9.0/webapps/springmvc;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
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://tomcat;
}
#使用正则表达式拦截后缀名为js、css的静态资源
location ~.*\.(js|css)?$ {
#静态资源放置的位置
root /opt;
#缓存过期时间
expires 1d;
}
}

遇到问题

1 Tomcat+Nginx实现动静分离的功能,动态请求为什么没有发到Tomcat这里?

参考&引用

nginx documentation
Nginx搭建反向代理服务器过程详解
使用Nginx实现负载均衡
tomcat 与 nginx,apache的区别是什么?

结语

写的比较简略,不成章法,接下来会慢慢完善

更新时间

发布时间 : 2016-08-27

看你的了!