Apache subdomain redirect into Tomcat

2019-02-03 20:29发布

问题:

I'm pretty new to Apache HTTP, and sysadmin-ing in general, so i have this question I have a domain (www.doamin.com) with an Apache listening to port 80, also I have an Apache Tomcat on the same domain configured to port 8080.

Is there a way to configure a subdomain (i.e, tomcat.domain.com) so it will redirect into my tomcat specific application, so user can access applications through app1.domain.com and app2.domain.com (and it will be served by Tomcat)?

I've seen a lot of mentioning to

mod_jk

and

mod_proxy

but all of the post assumed prior knowledge with Apache. can someone walk me thorugh?

Many thanks, -PK.

回答1:

mod_jk is outdated. It is recomended to use mod_proxy (mod_proxy_http or mod_proxy_ajp) to connect forward requests to your apache server to the tomcat.

  1. define a virtual host in your apache config
  2. create a proxy directive that forwards your requests to tomcat

Maybe this SO question give you some hints.

You can define two virtual hosts (app1.domain.tld and app2.domain.tld) that have proxy definitions for their designated apps. Example for app1:

<VirtualHost *:80>
    ServerName app1.domain.tld
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:8080/app1
    ProxyPassReverse / http://localhost:8080/app1
</VirtualHost> 


回答2:

while Magomi was almost right,

Presenting an exact way to do it.

  1. Add your subdomain to the DNS server

  2. integrate *mod_proxy* into httpf.conf :

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
  1. define two virtual hosts as following

    NameVirtualHost *:80

    <VirtualHost *:80>
        ServerName application.domain.com
        ProxyRequests Off
        ProxyPreserveHost On
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
        ProxyPass / http://www.domain.com:8080/application/
        ProxyPassReverse / http://www.domain.com:8080/application/
    </VirtualHost>
    
    <VirtualHost *:80>
        DocumentRoot C:\<pathToApache>\www
        ServerName www.domain.com
    </VirtualHost>
    

This will direct your site (www.domain.com) to your Apache HTTP server, and redirect all calls to Application to the Tomcat.

Hope this Helps,

-PK