首页 > 代码库 > tomcat结合httpd和nginx

tomcat结合httpd和nginx

httpd结合tomcat:

前提:httpd版本2.4以上,编译安装

httpd:192.168.223.136 tomcat:192.168.223.146

tomcat简单创建一个额外的webapps:

  [root@wadeson tomcat]# mkdir /data/webapps/{ROOT,WEB-INF,META-INF,classes,lib} -pv
  mkdir: 已创建目录 "/data"
  mkdir: 已创建目录 "/data/webapps"
  mkdir: 已创建目录 "/data/webapps/ROOT"
  mkdir: 已创建目录 "/data/webapps/WEB-INF"
  mkdir: 已创建目录 "/data/webapps/META-INF"
  mkdir: 已创建目录 "/data/webapps/classes"
  mkdir: 已创建目录 "/data/webapps/lib"
  vim /data/webapps/ROOT/index.jsp
 
  <Engine name="Catalina" defaultHost="192.168.223.146"> 替换默认的host主机
    <Host name="192.168.223.146" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="ROOT" reloadable="true"/>
      <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
          prefix="192.168.223.146_access_log" suffix=".txt"
          pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
  [root@wadeson data]# tree webapps/
  webapps/
  ├── classes
  ├── lib
  ├── META-INF
  ├── myapp
  │   └── login.jsp
  ├── ROOT
  │   └── index.jsp
  └── WEB-INF
 
httpd实现tomcat反代的几种方式:
1、基于http协议
  需要使用的模块:主模块:proxy_module、子模块:proxy_module_http
  1、在主配置文件httpd.conf开启下面两个模块:
  LoadModule proxy_module modules/mod_proxy.so
  LoadModule proxy_http_module modules/mod_proxy_http.so
  2、开启后打开虚拟主机的支持文件:
  # Virtual hosts
  Include /etc/httpd2.4/extra/httpd-vhosts.conf
  3、然后编辑vhosts文件:
  <VirtualHost *:80>
    ServerName 192.168.223.136              这里只是httpd服务器的ip
    proxyVia On                     如果设置为On每个请求和应答都会对应当前主机得到一个"Via:"头。(此选项可以不用设置)
    ProxyRequests Off           关闭正向代理
    ProxyPreserveHost On       当启用时,此选项将把传入请求的"Host:"行传递给被代理的主机,而不是传递在ProxyPass中指定的主机名,默认为off(此选项可以不用设置)
    <Proxy *>        设置代理权限,所有的都给代理至后端服务器
      Require all granted
    </Proxy>
    ProxyPass / http://192.168.223.146:8080/           
    ProxyPassReverse / http://192.168.223.146:8080/
    <Location />
      Require all granted
    </Location>
  </VirtualHost>
 
2、httpd与tomcat结合使用ajp协议:
  1、开启必须要支持的proxy模块
  LoadModule proxy_module modules/mod_proxy.so
  LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
 
  2、开启后打开虚拟主机的支持文件:
  # Virtual hosts
  Include /etc/httpd2.4/extra/httpd-vhosts.conf
 
  3、然后编辑vhosts文件:
  <VirtualHost *:80>
    ServerName 192.168.223.136
    proxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
      Require all granted
    </Proxy>
    ProxyPass / ajp://192.168.223.146:8009/            ajp协议开启的是8009端口
    ProxyPassReverse / ajp://192.168.223.146:8009/
    <Location />
      Require all granted
    </Location>
  </VirtualHost>
观察tomcat日志看见真实的ip:

[root@wadeson logs]# tail -f 192.168.223.146_access_log.2017-08-09.txt
192.168.223.1 - - [09/Aug/2017:11:31:49 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:31:49 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:31:49 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:32:24 +0800] "GET / HTTP/1.1" 200 122
192.168.223.1 - - [09/Aug/2017:11:47:51 +0800] "GET / HTTP/1.1" 200 122
192.168.223.1 - - [09/Aug/2017:11:47:52 +0800] "GET / HTTP/1.1" 200 122
192.168.223.1 - - [09/Aug/2017:11:47:54 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:48:13 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:48:13 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127
192.168.223.1 - - [09/Aug/2017:11:48:14 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 127

 

nginx与tomcat进行结合:

nginx:192.168.223.136,tomcat:192.168.223.146

server {
  listen 80;
  server_name 192.168.223.136;

  location / {

    root html;
    index index.html index.htm;
    proxy_pass http://192.168.223.146:8080;
  }

技术分享

技术分享

nginx日志如下:

192.168.223.1 - - [09/Aug/2017:13:43:26 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 112 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.223.1 - - [09/Aug/2017:13:43:30 +0800] "GET / HTTP/1.1" 200 107 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.223.1 - - [09/Aug/2017:13:46:11 +0800] "GET /myapp/login.jsp HTTP/1.1" 200 112 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"

tomcat日志如下:

192.168.223.136 - - [09/Aug/2017:13:43:26 +0800] "GET /myapp/login.jsp HTTP/1.0" 200 127
192.168.223.136 - - [09/Aug/2017:13:43:30 +0800] "GET / HTTP/1.0" 200 122
192.168.223.136 - - [09/Aug/2017:13:46:11 +0800] "GET /myapp/login.jsp HTTP/1.0" 200 127

如何才能使tomcat抓捕到真实的客户端ip呢?

1、在nginx上设置:

  proxy_set_header X-Forwarded-For $remote_addr;

2、然后在tomcat日志定义处添加首部信息:

  pattern="%h %{X-Forwarded-For}i %l %u %t &quot;%r&quot; %s %b" />

然后tomcat访问日志:

192.168.223.136 192.168.223.1 - - [09/Aug/2017:13:55:03 +0800] "GET /myapp/login.jsp HTTP/1.0" 200 127
192.168.223.136 192.168.223.1 - - [09/Aug/2017:13:55:04 +0800] "GET /myapp/login.jsp HTTP/1.0" 200 127

 

nginx结合tomcat实现动静分离:

location ~* \.(jsp|do)$ {
  proxy_pass http://192.168.223.146:8080;
}

访问网址:

技术分享

默认首页是index.html所以访问的是nginx自己的html下的index.html

技术分享

当后缀为jsp时,就请求访问tomcat了,在tomcat日志那里也可以查看:

192.168.223.136 - - - [09/Aug/2017:14:09:03 +0800] "GET /index.jsp HTTP/1.0" 200 122

 

 

利用nginx作为代理服务器,转向192.168.223.146的httpd服务器,然后通过httpd转向本地的tomcat

nginx:192.168.223.136   httpd:192.168.223.146,版本2.4以上  tomcat:192.168.223.146

配置httpd:同以上

1、基于http协议
  需要使用的模块:主模块:proxy_module、子模块:proxy_module_http
  1、在主配置文件httpd.conf开启下面两个模块:
  LoadModule proxy_module modules/mod_proxy.so
  LoadModule proxy_http_module modules/mod_proxy_http.so
  2、开启后打开虚拟主机的支持文件:
  # Virtual hosts
  Include /etc/httpd2.4/extra/httpd-vhosts.conf
  3、然后编辑vhosts文件:
  <VirtualHost *:80>
    ServerName 192.168.223.146            httpd和tomcat位于同一主机
    proxyVia On                     
    ProxyRequests Off           
    ProxyPreserveHost On      
    <Proxy *>        
      Require all granted
    </Proxy>
    ProxyPass / http://192.168.223.146:8080/           
    ProxyPassReverse / http://192.168.223.146:8080/
    <Location />
      Require all granted
    </Location>
  </VirtualHost>
技术分享

http日志:

192.168.223.136 - - [09/Aug/2017:14:37:09 +0800] "GET /index.jsp HTTP/1.0" 200 122

tomcat日志:

192.168.223.146 192.168.223.136 - - [09/Aug/2017:14:37:09 +0800] "GET /index.jsp HTTP/1.1" 200 122

技术分享

nginx只是做反代,没有处理请求的义务,由于后端httpd是直接反带的所以httpd默认的html下面的index.html不是tomcatwebapps下面的index.html

在tomcat的应用目录/data/webapps/ROOT下面创建一个index.html

cp index.html /data/webapps/ROOT/,然后再次访问:

技术分享

查看httpd日志:

192.168.223.136 - - [09/Aug/2017:14:41:41 +0800] "GET /index.html HTTP/1.0" 200 45

查看你tomcat日志:

192.168.223.146 192.168.223.136 - - [09/Aug/2017:14:41:41 +0800] "GET /index.html HTTP/1.1" 200 45

 

tomcat结合httpd和nginx