Apache+mod_jk+Tomcat (5)-----Apache port-based virtual host

Virtual Host

 

Concept: Building multiple website service technologies belonging to different domain names or based on different IPs on the same machine can assign different IPs and ports to each website running on the same physical machine, or allow multiple websites to have different domain name.

Create virtual hosts on demand

 

1. Demand

Two sets of applications running on the same host are distinguished by ports (port-based virtual hosts) (one set of programs is through port 50001, and the other set is through port 50002), based on the previously built environment.

 

2. Implementation content

1) Because the virtual host is port-based, modify the configuration file /etc/httpd/conf/httpd.conf

Listen 80

Listen 50001

Listen 50002

 

2) Define the configuration file of the virtual host

 

Configuration file for the first virtual host

/etc/httpd/conf.d/virtual.d/virtual1.conf

<VirtualHost *:50001>
   ServerName 192.168.100.29
   DocumentRoot "/var/www/html_virtual1"
   <Directory /var/www/html_virtual1>
     Options  FollowSymLinks
     AllowOverride None
     Require all granted
   </Directory>
   CustomLog logs/access_virtual1_log combined env=!nolog
   ErrorLog logs/error_virtual1.log
   ErrorDocument 400 /error/40X.json
   ErrorDocument 401 /error/40X.json
   ErrorDocument 403 /error/40X.json
   ErrorDocument 404 /error/40X.json
   ErrorDocument 405 /error/40X.json
   ErrorDocument 408 /error/40X.json
   ErrorDocument 410 /error/40X.json
   ErrorDocument 411 /error/40X.json
   ErrorDocument 412 /error/40X.json
   ErrorDocument 413 /error/40X.json
   ErrorDocument 414 /error/40X.json
   ErrorDocument 415 /error/40X.json
   ErrorDocument 500 /error/50X.json
   ErrorDocument 501 /error/50X.json
   ErrorDocument 502 /error/50X.json
   ErrorDocument 503 /error/50X.json
   ErrorDocument 506 /error/50X.json
   Alias /error/ "/var/www/error_virtual1/"
   IncludeOptional conf.d/mod_jk_virual1.conf
 </VirtualHost>

Configuration file for the second virtual host

 

/etc/httpd/conf.d/virtual.d/virtual2.conf

<VirtualHost *:50002>
   ServerName 192.168.100.29
   DocumentRoot "/var/www/html_virtual2"
   <Directory /var/www/html_virtual2>
     Options  FollowSymLinks
     AllowOverride None
     Require all granted
   </Directory>
   CustomLog logs/access_virtual2_log combined env=!nolog
   ErrorLog logs/error_virtual2.log
   ErrorDocument 400 /error/40X.json
   ErrorDocument 401 /error/40X.json
   ErrorDocument 403 /error/40X.json
   ErrorDocument 404 /error/40X.json
   ErrorDocument 405 /error/40X.json
   ErrorDocument 408 /error/40X.json
   ErrorDocument 410 /error/40X.json
   ErrorDocument 411 /error/40X.json
   ErrorDocument 412 /error/40X.json
   ErrorDocument 413 /error/40X.json
   ErrorDocument 414 /error/40X.json
   ErrorDocument 415 /error/40X.json
   ErrorDocument 500 /error/50X.json
   ErrorDocument 501 /error/50X.json
   ErrorDocument 502 /error/50X.json
   ErrorDocument 503 /error/50X.json
   ErrorDocument 506 /error/50X.json
   Alias /error/ "/var/www/error_virtual2/"
   IncludeOptional conf.d/mod_jk_virual2.conf
 </VirtualHost>

 

3) In order to load these two configuration files when apache starts, append the following configuration to /etc/httpd/conf/httpd.conf

IncludeOptional conf.d/virtual.d/*.conf

 

4) Create two document directories of virtual hosts respectively, and put the html resources that need to be accessed in them

mkdir / var / www / html_virtual1

virutal1test.html

mkdir / var / www / html_virtual2

 virutal2test.html

 

5) From the configuration files of the respective virtual hosts above, their respective mod_jk files are defined, so to create a mod_jk file, the worker still uses the previously defined tomat1.

 

/etc/httpd/conf.d/mod_jk_virtual1.conf

<IfModule mod_jk.c>

JkMount /virtual1/* tomcat1

</IfModule>

 

/etc/httpd/conf.d/mod_jk_virtual2.conf

<IfModule mod_jk.c>

JkMount /virtual2/* tomcat1

</IfModule>

 

6) Put two war packages in tomcat, one named virutal1, virtual2

 

7) Restart apache, restart tomcat

systemctl restart httpd

 

/opt/tomcat/bin/shutdown.sh

/opt/tomcat/bin/startup.sh

 

8) Test whether the static resources of apache can be accessed normally

http://192.168.100.29:50001/virtual1test.html

Result: normal display

 

http://192.168.100.29:50002/virtual2test.html

Result: normal display

 

9) Confirm log

/var/log/httpd/access_virtual1_log

10.0.210.135 - - [03/Aug/2017:15:34:59 +0900] "GET /virtual1test.html HTTP/1.1" 200 53 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"

 

/var/log/httpd/access_virtual2_log

10.0.210.135 - - [03/Aug/2017:15:45:54 +0900] "GET /virtual2test.html HTTP/1.1" 200 53 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"

 

10) Access tomcat resources through apache&mod_jk&tomcat

http://192.168.100.29:50001/virtual1/index.jsp

Result: normal display

 

http://192.168.100.29.50002/virtual2/index.jsp

Result: normal display

 

11) Confirm log

/var/log/httpd/access_virtual1_log

10.0.210.135 - - [03/Aug/2017:15:52:15 +0900] "GET /virtual1/index.jsp HTTP/1.1" 200 290 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"

 

/var/log/httpd/access_virtual2_log

10.0.210.135 - - [03/Aug/2017:15:52:55 +0900] "GET /virtual2/index.jsp HTTP/1.1" 200 286 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"

 

Summary: Generally speaking, the configuration is very smooth, but for some concepts, I still need to learn, such as the concept of ServerName, I do not particularly understand. See the explanation in httpd.conf is this explanation

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#

If your host doesn't have a registered DNS name, enter its IP address here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403830&siteId=291194637