本文章內容:
1. NT反代
2. AT反代
2.1 Apache使用HTTP協議反代
2.2 Apache使用AJP協議反代
根據Tomcat自己的部署環境不同,到達Tomcat的請求有可能有兩類,如果Tomcat直接面向客戶端那麼很顯然這個請求會來自於瀏覽器的,各種各樣的用戶代理;如果Tomcat是被反代服務器代理請求的話,那麼到達Tomcat的請求都應該來自於代理服務器的。
環境:
1. Proxy主機 (前端)
[root@proxy ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@proxy ~]# ip a | egrep "(ens33|ens37)"
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.169.254/24 brd 192.168.169.255 scope global noprefixroute ens33 #對內IP
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.0.21/24 brd 192.168.0.255 scope global noprefixroute dynamic ens37 #對外IP
2. Tomcat主機 (後端)
[root@tomcat-1 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@tomcat-1 ~]# ip a | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.169.131/24 brd 192.168.169.255 scope global noprefixroute ens33 #內部IP
1. NT反代
前端使用Nginx當Proxy主機,使用HTTP協議反代至後端Tomcat主機。
1.1 安裝、設定Tomcat主機
1.1.1 安裝Tomcat
[root@tomcat-1 ~]# yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps -y
[root@tomcat-1 ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
1.1.2 啟動Tomcat服務並設定開機自啟動
[root@tomcat-1 ~]# systemctl start tomcat.service
[root@tomcat-1 ~]# systemctl enable tomcat.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
[root@tomcat-1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 [::]:8009 #AJP協議的port [::]:*
LISTEN 0 100 [::]:8080 #HTTP協議的port [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 #Server實例管理的port [::]:*
...
1.1.3 訪問192.168.169.131:8080
1.2 安裝、設定Proxy主機
1.2.1 安裝Nginx
[root@proxy ~]# yum install nginx -y
1.2.2 設定訪問www.ilinux.io虛擬主機的80 port會反代至後端192.168.169.131:8080
[root@proxy ~]# vim /etc/nginx/conf.d/www-ilinux-io_http_tomcat.conf
server {
listen 80;
server_name www.ilinux.io;
location / {
proxy_pass http://192.168.169.131:8080;
}
}
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
1.2.3 啟動Nginx服務並設定開機自啟動
[root@proxy ~]# systemctl start nginx.service
[root@proxy ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@proxy ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:80 [::]:*
1.2.4 訪問www.ilinux.io
2. AT反代
前端使用Apache當Proxy主機,使用HTTP或AJP協議反代至後端Tomcat主機。
2.1 Apache使用HTTP協議反代
2.1.1 安裝、設定Tomcat主機
同1.1。
2.1.2 安裝、設定Proxy主機
2.1.2.1 安裝Apache
[root@proxy ~]# yum install httpd -y
2.1.2.2 檢查是否有裝載及啟用proxy_module、proxy_http_module模塊 (proxy_module模塊主要是實現反代功能的,proxy_http_module模塊則是實現HTTP協議反代)
[root@proxy ~]# httpd -M
proxy_module (shared)
proxy_http_module (shared)
...
2.1.2.3 設定訪問www.ilinux.io虛擬主機的80 port會反代至後端192.168.169.131:8080
[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_http_tomcat.conf
<VirtualHost *:80>
ServerName www.ilinux.io
ProxyRequests off #關閉正向代理
ProxyPreserveHost on #將請求報文中的host首部一併傳到後端去,這樣後端主機可以基於host首部來判定要傳到哪台虛擬主機上去
ProxyVia on #在響應報文中加上via首部
<Proxy *> #定義代理功能能夠被那些用戶訪問,"*"表示所有的proxy功能
Require all granted
</Proxy>
ProxyPass / http://www.ilinux.io:8080/ #定義把"/"這個url反代到後端www.ilinux.io主機上的"/"這個url
ProxyPassReverse / http://www.ilinux.io:8080/ #如果後端主機返回了一個url重定向,則這個url重定向的結果也響應給客戶端知道
<Location /> #定義"/"這個url能夠被那些用戶訪問
Require all granted
</Location>
</VirtualHost>
[root@proxy ~]# httpd -t #檢查配置文件語法有無錯誤
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::12da:f368:4b08:ba51. Set the 'ServerName' directive globally to suppress this message #將主機名設定為FQDN這個訊息就會消失
Syntax OK
2.1.2.4 啟動Apache服務並設定開機自啟動
[root@proxy ~]# systemctl start httpd.service
[root@proxy ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@proxy ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 [::]:80 [::]:*
2.1.2.5 訪問www.ilinux.io
2.2 Apache使用AJP協議反代
2.2.1 安裝、設定Tomcat主機
同1.1。
2.2.2 安裝、設定Proxy主機
2.2.2.1 安裝Apache
[root@proxy ~]# yum install httpd -y
2.2.2.2 檢查是否有裝載及啟用proxy_module、proxy_ajp_module模塊 (proxy_module模塊主要是實現反代功能的,proxy_ajp_module模塊則是實現AJP協議反代)
[root@proxy ~]# httpd -M
proxy_module (shared)
proxy_ajp_module (shared)
...
2.2.2.3 設定訪問www.ilinux.io虛擬主機的80 port會反代至後端192.168.169.131:8080
[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_ajp_tomcat.conf
<VirtualHost *:80>
ServerName www.ilinux.io
ProxyRequests off
ProxyPreserveHost on
ProxyVia on
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://192.168.169.131:8009/ #使用AJP協議代理到後端,後端AJP協議的port為8009
ProxyPassReverse / ajp://192.168.169.131:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@proxy ~]# httpd -t
Syntax OK
2.2.2.4 啟動Apache服務並設定開機自啟動
[root@proxy ~]# systemctl start httpd.service
[root@proxy ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@proxy ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 [::]:80 [::]:*
2.2.2.5 訪問www.ilinux.io