tomcat结合httpd和nginx

2021-06-07 13:02

阅读:659

[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 "%r" %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文件:
  
    ServerName 192.168.223.146            httpd和tomcat位于同一主机
    proxyVia On                     
    ProxyRequests Off           
    ProxyPreserveHost On      
            
      Require all granted
    
    ProxyPass / http://192.168.223.146:8080/           
    ProxyPassReverse / http://192.168.223.146:8080/
    
      Require all granted
    
  
技术分享

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

 


评论


亲,登录后才可以留言!