您的位置:首页 > 运维架构 > 反向代理

Nginx 反向代理 Tomcat

2017-08-01 23:01 337 查看


Nginx 反向代理 Tomcat

Nginx 是一个很强大的反向代理服务器,在JavaWeb项目里,Nginx 和 Apache httpd 经常会被最为前端服务器使用,负责转发请求,实现负载均衡,再配合Tomcat,Jetty 等JavaWeb服务器使用,负责实际处理请求;

与Apache httpd 相比,Nginx更加轻量化,占用更少资源,同时具有更强的并发性(单Apache一般4000,单Nginx上一般50000),更强大的静态资源处理能力,同时Nginx在配置要比Apache要方便地太多了;

以下演示了简单的单配置文件形式和多配置文件形式;

为了方便说明,这2种配置形式都是Nginx将全部的请求全部转发到Tomcat,Tomcat在处理静态资源上的效率是远远比不上Nginx的,无法发挥 Nginx 处理静态资源上的优势,关于配置 Nginx和其它服务器 的动静分离,负载均衡,参见以下:

Nginx 动静分离:http://blog.csdn.net/al_assad/article/details/76561991

Nginx 反向代理Tomcat集群,负载平衡:http://blog.csdn.net/al_assad/article/details/76562178


最简单的演示

关于Nginx安装配置参考:Nginx
关于JDK,Tomcat安装配置:JDK
4000
和 Tomcat
假设Nginx要将处理请求转发给Tomcat里的testapp,根目录路径为 /tomcat/webapps/testapp;
     请求的域名为“test.assad.site";
     Tomcat和Nginx位于同一个机器上;
1)Nginx转发配置

假设Nginx路径为 /nginx,vim编辑 /nginx/conf/nginx.conf,在http节点下添加以下内容:

1
http {
2
   ......
3
   server {
4
      listen 80;
5
      server_name test.assad.site;                #原始请求URL
6
7
      location / {                            #转发所有请求给Tomcat
8
         proxy_pass http://127.0.0.1:8080/testapp;    #转发给Tomcat的uri路由
9
         proxy_set_header Host $http_addr;
10
         proxy_set_header X-Real-IP $remote_addr;
11
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12
      }
13
   }
14
}

2)Tomcat配置Host

假设Tomcat路径为 /tomcat ,vim 编辑 /tomcat/conf/server.conf ,在Server/Server/Engine/ 节点路径下,添加<Host>节点;



1

<Host name="test.assad.site"  appBase="/tomcat/webapps/testapp"  unpackWARs="true" autoDeploy="true">


2

   <Context path="" docBase="/tomcat/webapps/testapp" debug="0" reloadable="true" />


3

   <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"


4

        prefix="localhost_access_log." suffix=".txt"


5

        pattern="%h %l %u %t "%r" %s %b" />


6

</Host>


7



其中name字段为请求URL,appBase字段为该URL转发到的 web application 的更目录;

如果Tomcat服务器的所有请求都有Nginx转发,不再需要使用 ip:8080 的形式请求,为了安全起见,可以将<Host name="localhost" appBase="webapps">节点及其子节点注释掉,此时Tomcat服务器只能通过Nginx请求转发的形式访问;
之后重启Tomcat和nginx,通过浏览器器访问:http://test.assad.site 就可以访问到Tomcat的testapp ;


多配置文件方式

如果一个Nginx服务器代理 Tomcat 的多个 JavaWeb 应用,这些 JavaWeb 应用分别响应不同的URL请求,一般的做法是将 nginx 的 nginx.conf 分拆为多个 conf 文件,每个conf 文件管理一个JavaWeb的代理配置,这样更加方便也更加安全对各个JavaWeb进行Nginx的代理配置;

假设Nginx和Tomcat位于同一个机器上,Nginx 分别转发"test1.assad.site","test2.assad.site" 给Tomcat上的 testapp1,testapp2 这两个JavaWeb 应用;
这2个JavaWeb app根目录的路径分别是:/tomcat/webapps/testapp1,/tomcat/webapps/testapp2
Nginx根目录为 /nginx

以下是配置过程:


1)Nginx转发配置

修改 /nginx/conf/nginx.conf, 引入所有vhost的配置信息,类似如下; 

1
http{
2
   ......
3
   include vhost/*.conf;
4
}
在 /nginx/conf/vhost 下创建“test1.assad.site.conf”"test2.assad.site.conf" 两个文件,以"test1.assad.site.conf"的配置示例,内容如下:
test1.assad.site.conf

1

upstream testapp1 {       #设置一个upstream


2

   server 127.0.0.1:8080;


3

}


4

server {


5

   listen 80;                   #监听的系统端口


6

   server_name test1.assad.site;    #代理转发的原始URL


7

   charset utf-8;                #字符编码


8



9

   root /tomcat/webapps/testapp1;         # 相应webapp的根目录


10

   index index.html index.htm index.jsp    #起始页


11

   


12

    error_page 404 /errpage/404.html;       # http错误页路径


13

   error_page 500 502 503 504 /errpage/50x.html;


14

   


15

    location / {       #转发所有的请求


16

      proxy_pass http://testapp1;       #代理转发的upstream,格式http://upstream_name


17

      proxy_set_header Host $http_host;


18

      proxy_set_header X-Real-IP $remote_addr;


19

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


20

      


21

   } 


22

}


test2.assad.site.conf 配置类似以上


2)Tomcat配置Host

在 Tomcat 的 server.xml 中的Server/Service/Engine/ 节点下,添加以下<Host>节点:

1

....


2

<Host name="test1.assad.site"  appBase="/tomcat/webapps/testapp1"  unpackWARs="true" autoDeploy="true">


3

   <Context path="" docBase="/tomcat/webapps/testapp1" debug="0" reloadable="true" />


4

   <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"


5

        prefix="localhost_access_log." suffix=".txt"


6

        pattern="%h %l %u %t "%r" %s %b" />


7

</Host>


8

<Host name="test2.assad.site"  appBase="/tomcat/webapps/testapp2"  unpackWARs="true" autoDeploy="true">


9

   <Context path="" docBase="/tomcat/webapps/testapp2" debug="0" reloadable="true" />


10

   <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"


11

        prefix="localhost_access_log." suffix=".txt"


12

        pattern="%h %l %u %t "%r" %s %b" />


13

</Host>


14

....


之后重启 Nginx 和 Tomcat 使配置生效就ok了;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: