Http和Nginx反代至Tomcat(LNMT、LAMT)

Http和Nginx反代至Tomcat(LNMT、LAMT)html

================================================================================前端

概述:java

   本章將主要介紹Http和Nginx反代至Tomcat,具體內容以下:node

  • LNMT部署並實現動靜分離nginx

  • LAMT部署的實現方式;web

     ·proxy_http_module代理配置數據庫

      ·proxy_ajp_module代理配置bootstrap

  • Tomcat腳本(啓動、中止、重啓)vim

================================================================================後端

LNMT:Linux Nginx MySQL Tomcat 

工做流程(結合方式):

  • Client (http) --> nginx (reverse proxy)(http) --> tomcat  (http connector)



實驗:LNMT部署並實現動靜分離


環境描述:

  • 虛擬機node1(CentOS 7系統)即做爲nginx服務器,又做爲Tomcat服務器,客戶端發來的請求首先經由nginx處理,若是爲靜態內容則直接由nginx響應,若是爲動態內容,則由nignx反代至後端的Tomcat服務器;

實驗步驟以下:

 1.在node1服務器上安裝nginx,這裏我使用官方下載的rpm包直接安裝,以下:

[root@centos7 ~]# cd nginx
[root@centos7 nginx]# ls
 nginx-1.10.0-1.el7.ngx.x86_64.rpm  
[root@centos7 ~]# yum install -y ./nginx-1.10.0-1.el7.ngx.x86_64.rpm

 2.編輯nginx的配置文件/etc/nginx/conf.d/default.conf,在虛擬主機server中定義來自客戶端請求的全部內容所有反代至後端的Tomcat服務器,以下:

# 我這裏的兩個虛擬主機是基於主機名定義的,因此ip相同,要想在nginx中選擇反代到後端的虛擬主機,首先要定以tomcat 使用的默認虛擬主機)
[root@centos7 ~]# vim /etc/nginx/conf.d/default.conf
 location / {
     proxy_pass http://location:8080;
 } 
 或者:
 location / {
     proxy_pass http://node1.taotao.com:8080;
 }

 3.保存退出後檢查nginx語法,沒問題後啓動nginx服務,以下:

[root@centos7 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos7 conf.d]# nginx 
[root@centos7 conf.d]# ss -tnl 
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
LISTEN      0      25                                                      *:514                                                                 *:*                  
LISTEN      0      128                                                     *:80                                                                  *:*                  
LISTEN      0      128                                                     *:22                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
LISTEN      0      25                                                     :::514                                                                :::*                  
LISTEN      0      1                                        ::ffff:127.0.0.1:8005                                                               :::*                  
LISTEN      0      100                                                    :::8009                                                               :::*                  
LISTEN      0      100                                                    :::8080                                                               :::*                  
LISTEN      0      128                                                    :::22                                                                 :::*                  
LISTEN      0      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*

 4.在瀏覽器中直接請求,能夠正常訪問資源,以下:

   在tomcat中定義默認虛擬主機爲 node1,而後在nginx中定義反代至node1的虛擬主機

wKiom1hmc9igsYjyAAA5C9FRvjw908.png

  反代至 localhost 虛擬主機

wKioL1hmc92jb0miAAGxP-yr4Aw483.png

 在自定義的Tomcat的虛擬主機的目錄中/data/webapps/ROOT中添加一張靜態資源圖片,也能夠正常訪問,以下:

wKiom1hmdruQfLT9AA6UDyvjJXw428.png



動靜分離: 

 如上是把整個客戶端請求的內容不管動靜都反代至後端的服務器,如今經過在nginx的配置文件中定義實現動靜分離,即:靜態資源由nginx直接響應,動態資源由Tomcat響應。nginx配置文件修改以下:

# 在server中從新定義一個location,把jsp、do結尾的反代至後端Tomcat主機
location ~* \.(jsp|do)$ {
     proxy_pass http://node1.taotao.com:8080;
 }

 檢測nginx語法,重載nginx服務

[root@centos7 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos7 conf.d]# nginx -s reload

 在瀏覽器中分別請求靜態資源和動態資源,發現由於只把動態資源代理到後端Tomcat服務,因此只有動態資源有響應,靜態資源提示沒有發現,以下:

wKiom1hmekDCyaSZAABf-9JYkaQ946.png

wKioL1hmekHTa2xfAAA-dWT6Cek841.png

 如今,咱們在nginx的映射根目錄下提供靜態資源1.jpg,再次訪問能夠發現能夠被正常加載到,說明是由前端nginx服務器直接響應的,以下:

[root@centos7 ROOT]# mv 1.jpg /usr/share/nginx/html/
[root@centos7 ROOT]# ls /usr/share/nginx/html/
1.jpg   error_pages  forum  index.html  messages.html

wKiom1hme06zOScsAAwgbcPOZwM262.png


 如上,把響應的java程序部署到指定的目錄便可,若是須要鏈接後端數據庫,只須要受權一個帳戶便可。這就是LNMT部署的整個過程。。。



LAMT:Linux Apache(httpd) MySQL Tomcat 

httpd的代理模塊:

  • proxy_module:反代模塊

  • proxy_http_module:適配http協議客戶端;

  • proxy_ajp_module:適配ajp協議客戶端;

工做流程(結合方式):

  • Client (http) --> httpd (proxy_http_module)(http) --> tomcat  (http connector)

  • Client (http) --> httpd (proxy_ajp_module)(ajp) --> tomcat  (ajp connector)

  • Client (http) --> httpd (mod_jk)(ajp) --> tomcat  (ajp connector)



 實驗:使用httpd的proxy_module(包括:proxy_http_module和proxy_ajp_module)模塊結合Tomcat,反代來自客戶端或者前端獨立反代服務器(nginx等)的請求


第一種方式:proxy_http_module代理配置示例:

1.首先安裝httpd服務器,編輯httpd的配置文件,在/etc/httpd/conf.d中定義一個虛擬主機,配置使用proxy_module和proxy_http_module模塊反代,以下:

[root@centos7 httpd]# vim conf.d/http_tomcat.conf
<VirtualHost *:80>
    ServerName      node1.taotao.com
    ProxyRequests   Off         # 關閉正向代理
    ProxyVia        On     # 添加由誰代理轉發
    ProxyPreserveHost On          # 反代時要不要保留客戶端請求的主機名,併發送給tomcat
    <Proxy *>
       Require all granted 
    </Proxy>
    ProxyPass /  http://localhost:8080/  # 注意,這裏寫Tomcat的主機或者虛擬主機是沒有用的,要想訪問Tomcat的虛擬主機,取決於用戶在客戶端瀏覽器中鍵入的主機名 
    ProxyPa***everse / http://localhost:8080/
    <Location />
       Require all granted
    </Location>
</VirtualHost>

2.檢測語法,啓動httpd服務,在瀏覽器中請求,以下:

[root@centos7 conf.d]# httpd -t
Syntax OK

[root@centos7 conf.d]# systemctl start httpd
[root@centos7 conf.d]# ss -tnl
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN     0      25                     *:514                                *:*                  
LISTEN     0      128                    *:22                                 *:*                  
LISTEN     0      128            127.0.0.1:631                                *:*                  
LISTEN     0      100            127.0.0.1:25                                 *:*                  
LISTEN     0      128            127.0.0.1:6010                               *:*                  
LISTEN     0      25                    :::514                               :::*                  
LISTEN     0      1       ::ffff:127.0.0.1:8005                              :::*                  
LISTEN     0      100                   :::8009                              :::*                  
LISTEN     0      128                   :::80                                :::*                  
LISTEN     0      100                   :::8080                              :::*                  
LISTEN     0      128                   :::22                                :::*                  
LISTEN     0      128                  ::1:631                               :::*                  
LISTEN     0      100                  ::1:25                                :::*                  
LISTEN     0      128                  ::1:6010                              :::*                  
LISTEN     0      128                   :::443                               :::*

wKioL1hrVAuCrRYcAAGvMoR2UZc730.png

第二種方式:proxy_ajp_module代理配置示例:

 1.編輯httpd的配置文件,在/etc/httpd/conf.d中定義一個虛擬主機,配置使用proxy_module和proxy_http_module模塊反代,以下

[root@centos7 conf.d]# cp http_tomcat.conf ajp_tomcat.conf
[root@centos7 conf.d]# mv http_tomcat.conf http_tomcat.conf.bak 
[root@centos7 conf.d]# vim ajp_tomcat.conf 
[root@centos7 conf.d]# cat ajp_tomcat.conf 
<VirtualHost *:80>
    ServerName      tc1.taotao.com
    ProxyRequests   Off
    ProxyVia        On
    ProxyPreserveHost On
    <Proxy *>
       Require all granted 
    </Proxy>
    ProxyPass / ajp://192.168.1.112:8009/
    ProxyPa***everse / ajp://192.168.1.112:8009/
    <Location />
       Require all granted
    </Location>
</VirtualHost>

 2.在瀏覽器中鍵入不一樣的Tomcat主機名後,後端Tomcat響應不一樣的虛擬主機,以下:

wKioL1hrW2eBsrgnAAA4bTW4I_w229.png

wKioL1hrW2jzfQpyAAForVdad9M024.png


注意:

  • 在使用nginx作反代時,proxy_pass 保留主機名作反代項後端Tomcat請求,因此Tomcat可以根據反代時的主機名來斷定,前端反代服務器請求的是Tomcat的哪一個虛擬主機;

  • 可是,在使用httpd作反代時,不能經過反代(ProxyPass)時使用的主機名來斷定後端Tomcat使用什麼虛擬主機來響應。因此,這裏的ProxyPass和ProxyPa***everse使用主機名或者IP地址是沒有任何區別的,反代到後端都爲ip地址,Tomcat沒法判斷使用哪一個虛擬主機響應,只能使用默認的虛擬主機響應。爲了可以使後端Tomcat可以使用不一樣的虛擬主機來響應,就要打開ProxyPreserveHost,保留請求時的主機名,這時客戶端在瀏覽器中鍵入的主機名將會請求反代至後端的Tomcat,而後Tomcat分別對應不一樣的虛擬主機來響應請求。


如上,就是部署LAMT環境的整個過程。。。

wKiom1hrh4zA-fAyAADZEzEXzf0706.png



Tomcat 腳本啓動、中止、重啓

wKiom1hrh8zwCOXUAAByAf52Nts861.png


1.在/etc/rc.d/init.d下編寫tomcat的腳本

[root@centos7 ~]# vim /etc/rc.d/init.d/tomcat
 #!/bin/bash
 # Tomcat init script for Linux.
 #
 # chkconfig: 2345 96 14
 # description: The Apache Tomcat servlet/JSP container.
 # JAVA_OPTS='-Xms64m -Xmx128'
 JAVA_HOME=/usr/java/latest
 CATALINA_BASE=/usr/local/tomcat
 export JAVA_HOME CATALINA_BASE

 case $1 in
 start)
   exec $CATALINA_BASE/bin/catalina.sh start
   ;;
 stop)
   exec $CATALINA_BASE/bin/catalina.sh stop
   ;;
 restart)
   $CATALINA_BASE/bin/catalina.sh stop
   sleep 2
   exec $CATALINA_BASE/bin/catalina.sh start
   ;;
 *)
   echo "Usage: `basename $0` {start|stop|restart}"
   exit1
   ;;
 esac

2.添加到chkconfig中,並授予執行權限

[root@centos7 ~]# chkconfig --add tomcat
[root@centos7 ~]# chmod +x /etc/rc.d/init.d/tomcat

3.測試,重啓tomcat服務,以下:

[root@centos7 ~]# service tomcat restart
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
相關文章
相關標籤/搜索