tomcat结合httpd和nginx
Posted wadeson
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat结合httpd和nginx相关的知识,希望对你有一定的参考价值。
httpd结合tomcat:
前提:httpd版本2.4以上,编译安装
httpd:192.168.223.136 tomcat:192.168.223.146
tomcat简单创建一个额外的webapps:
On
每个请求和应答都会对应当前主机得到一个"Via:
"头。(此选项可以不用设置)ProxyPass
中指定的主机名,默认为off(此选项可以不用设置)[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:同以上
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的主要内容,如果未能解决你的问题,请参考以下文章
nginx/httpd + tomcat及负载均衡tomcat
apache httpd, nginx, tomcat, jboss