tomcat結合httpd和nginx

wadeson發表於2017-08-09

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

 

相關文章