tomcat代理的設定

weixin_33850890發表於2017-09-03

NT的實現

yum -y install nginx  安裝Nginx
cd /etc/nginx/conf.d/
vim tomcat.conf
server {
    listen 80;
    server_name www.chenxi.com;
    location / {
       proxy_pass http://127.0.0.1;   定義只要非.jsp與do檔案結尾的都訪問本機,也就是動靜分離
    }
    location ~* \.(jsp|do)$ {
    proxy_pass http://www.chenxis.com:8080;
    }
}
server {
        listen 80;
        server_name www.chenxit.com;
        location / {
        proxy_pass http://www.chenxid.com:8080;
        }
}

getenforce    檢視SELinux策略
setenforce 0  關閉SELinux策略
[root@localhost 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@localhost conf.d]#systemctl start nginx
[root@localhost conf.d]# ss -lnt
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:80                                           *:*                  
LISTEN      0      128                              *:22                                           *:*                  
LISTEN      0      100                      127.0.0.1:25                                           *:*                  
LISTEN      0      128                             :::80                                          :::*                  
LISTEN      0      100                             :::8080                                        :::*                  
LISTEN      0      128                             :::22                                          :::*                  
LISTEN      0      100                            ::1:25                                          :::*                  
LISTEN      0      1                 ::ffff:127.0.0.1:8005                                        :::*                  
LISTEN      0      100                             :::8009                                        :::*                  
[root@localhost conf.d]# vim /etc/hosts   配置本地host解析檔案

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.251.203 www.chenxis.com www.chenxid.com
------------------------------------------------------------------------------------------------------------
tomcat的設定
 <Host name="www.chenxid.com"  appBase="webapps"   預設tomcat主頁
            unpackWARs="true" autoDeploy="true">
     </Host>
     <Host name="www.chenxis.com"  appBase="/data/webapps"    定義一個網頁的目錄
            unpackWARs="true" autoDeploy="true">
     <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="chenxis_access_log." suffix=".log"
               pattern="%h %l %u %t "%r" %s %b" />
[root@centos7 tomcat]# cd /data/webapps
[root@centos7 webapps]# ls ROOT/
classes  index.jsp  lib  META-INF  WEB-INF

客戶端測試

6398706-6dbd155c7fb355dc.png

以.jsp結尾

6398706-6f29c9e7b2ca6de4.png

AT的實現

[root@localhost conf.d]#yum -y install httpd  安裝httpd服務
[root@localhost conf.d]# httpd -M 檢視httpd的模組
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set t
he 'ServerName' directive globally to suppress this messageLoaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 access_compat_module (shared)
 actions_module (shared)
 alias_module (shared)
 allowmethods_module (shared)
 auth_basic_module (shared)
 auth_digest_module (shared)
 authn_anon_module (shared)
 authn_core_module (shared)
 authn_dbd_module (shared)
 authn_dbm_module (shared)
 authn_file_module (shared)
 authn_socache_module (shared)
 authz_core_module (shared)
 authz_dbd_module (shared)
 authz_dbm_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_owner_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cache_module (shared)
 cache_disk_module (shared)
 data_module (shared)
 dbd_module (shared)
 deflate_module (shared)
 dir_module (shared)
 dumpio_module (shared)
 echo_module (shared)
 env_module (shared)
 expires_module (shared)
 ext_filter_module (shared)
 filter_module (shared)
 headers_module (shared)
 include_module (shared)
 info_module (shared)
 log_config_module (shared)
 logio_module (shared)
 mime_magic_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 remoteip_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 slotmem_plain_module (shared)
 slotmem_shm_module (shared)
 socache_dbm_module (shared)
 socache_memcache_module (shared)
 socache_shmcb_module (shared)
 status_module (shared)
 substitute_module (shared)
 suexec_module (shared)
 unique_id_module (shared)
 unixd_module (shared)
 userdir_module (shared)
 version_module (shared)
 vhost_alias_module (shared)
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
 lua_module (shared)
 mpm_prefork_module (shared)
 proxy_module (shared)   代理模組必須存在,沒有的話手動啟動
 lbmethod_bybusyness_module (shared)
 lbmethod_byrequests_module (shared)
 lbmethod_bytraffic_module (shared)
 lbmethod_heartbeat_module (shared)
 proxy_ajp_module (shared)   ajp的代理模組
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)  確保此模組存在
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)
 systemd_module (shared)
 cgi_module (shared)
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)
[root@localhost conf.d]# vim tomcat-http.conf 

<VirtualHost *:80>
                                ServerName      www.chenxi.com
                                ServerAlias     www.cx.com   定義一個別名
                                ProxyRequests Off 是否關閉正想代理off關閉
                                ProxyVia        On響應是是否加一個手部
                                ProxyPreserveHost Off   是否將客戶端請求主機名傳遞後端主機on傳遞off不傳遞
                                <Proxy *>
                                        Require all granted對本地所有代理功能都允許
                                </Proxy>
                                ProxyPass / http://www.chenxis.com:8080/表示你請求的內容是什麼後面內容就是什麼;建議一致,不一致重寫是會出問題
                                ProxyPassReverse / http://www.chenxis.com:8080/ 重寫後是否對新URL做代理
                                <Location />
                                        Require all granted做授權2.4要求必須要授權的
                                </Location>
                        </VirtualHost>


客戶端的host檔案


6398706-4ef57d0d703c61fe.png

客戶端測試


6398706-a2f08283457bd1f4.png

使用ajp協議實現at

[root@localhost conf.d]# vim tomcat-http.conf 

<VirtualHost *:80>
                                ServerName      www.chenxi.com
                                ServerAlias     www.cx.com
                                ProxyRequests Off
                                ProxyVia        On
                                ProxyPreserveHost Off
                                <Proxy *>
                                        Require all granted
                                </Proxy>
                                ProxyPass / ajp://www.chenxis.com:8005/
                                ProxyPassReverse / ajp://www.chenxis.com:8005/
                                <Location />
                                        Require all granted
                                </Location>
                        </VirtualHost>
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# ss -lnt
State      Recv-Q Send-Q Local Address:Port               Peer Addre
ss:Port              LISTEN     0      128     *:22                  *:*                 
 LISTEN     0      100    127.0.0.1:25                  *:*          
        LISTEN     0      128    :::80                 :::*                 
 LISTEN     0      128    :::22                 :::*                 
 LISTEN     0      100       ::1:25                 :::*             
     

客戶端測試不支援把後端主機,只會把前段使用者請求的主機名傳給後端主機

<VirtualHost *:80>
                                ServerName      www.chenxi.com
                                ServerAlias     www.cx.com
                                ProxyRequests Off
                                ProxyVia        On
                                ProxyPreserveHost Off
                                <Proxy *>
                                        Require all granted
                                </Proxy>
                                ProxyPass / ajp://www.chenxis.com:8009/
                                ProxyPassReverse / ajp://www.chenxis.com:8009/
                                <Location />
                                        Require all granted
                                </Location>
                        </VirtualHost>
               [root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# ss -lnt
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:22                                           *:*                  
LISTEN      0      100                      127.0.0.1:25                                           *:*                  
LISTEN      0      128                             :::80                                          :::*                  
LISTEN      0      128                             :::22                                          :::*                  
LISTEN      0      100                            ::1:25                                          :::*     

客戶端測試

6398706-20850c4d49b62900.png

更改host檔案

6398706-bffab72cfefe40a6.png

訪問www.chenxis.com

6398706-3603885ad5b38c6c.png

實現NAT用jsp

[root@localhost yum.repos.d]# yum -y install nginx
[root@localhost conf.d]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cat tomcat.conf  Nginx的相關設定
server {
    listen 80;
    server_name www.chenxi.com;
    location / {
       proxy_pass http://127.0.0.1;
    }
    location ~* \.(jsp|do)$ {
    proxy_pass http://www.chenxis.com:8080;
    }
}
server {
        listen 80;
        server_name www.chenxit.com;
        location / {
        proxy_pass http://www.chenxid.com:8080;
        }
}

[root@localhost conf.d]# vim tomcat-http.conf   httpd端使用ajp代理
<VirtualHost *:80>
                                ServerName      www.chenxi.com
                                ServerAlias     www.cx.com
                                ProxyRequests Off   關閉正向代理
                                ProxyVia        On
                                ProxyPreserveHost Off
                                <Proxy *>
                                        Require all granted
                                </Proxy>
                                ProxyPass / ajp:/ http.chenxis.com:8009/
                                ProxyPassReverse / http://www.chenxis.com:8009/
                                <Location />
                                        Require all granted
                                </Location>
                        </VirtualHost>

客戶端測試

6398706-6cc16b008bb0c211.png

實現NAT用httd的配置

http的相關配置
[root@localhost httpd]# vim /etc/httpd/conf.d/tomcat-http.conf 

<VirtualHost *:80>
                                ServerName      www.chenxi.com
                                ServerAlias     www.cx.com
                                ProxyRequests Off
                                ProxyVia        On
                                ProxyPreserveHost Off
                                <Proxy *>
                                        Require all granted
                                </Proxy>
                                ProxyPass / http://www.chenxis.com:8080/
                                ProxyPassReverse / http://www.chenxis.com:8080/
                                <Location />
                                        Require all granted
                                </Location>
                        </VirtualHost>
[root@localhost httpd]# systemctl restart httpd   重啟服務
Nginx服務不需要做什麼改動

客戶端測試

6398706-7faec69fdaa03a5b.png

NTjijue的實現


HT客戶測試
http://172.16.250.236:9009/haproxy?stats

6398706-42bb532f9ccf4888.png

相關文章