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