开发者

How to create a domain like "http://username.example.com" in a J2EE web application?

开发者 https://www.devze.com 2022-12-19 03:21 出处:网络
I am a web application developer, just a beginner. I am designing a small J2EE web application (for e开发者_如何学运维xample, the service name would be like http://www.example.com). It uses Apache Tom

I am a web application developer, just a beginner. I am designing a small J2EE web application (for e开发者_如何学运维xample, the service name would be like http://www.example.com). It uses Apache Tomcat. Spec: When a user sign up into the web application, he will get a custom domain like

http://username.example.com

How can i accomplish this in my web application. I am still developing the app. i have not hosted it yet.


You’ll need to create a virtual host for each subdomain in Apache configuration like:

<VirtualHost *:80>
ServerAdmin webmaster@subdomain1.example.com
DocumentRoot /www/subdomain1.example.com
ServerName subdomain1.example.com
ErrorLog logs/subdomain1.example.com-host.example.com-error_log
CustomLog logs/subdomain1.example.com-access_log common
</VirtualHost>

There is one problem though. If your solution requires folders creation, virtual host addition and apache restarts which does not look good to me. How would I deal with this problem is:

  1. Create a wildcard virtual host for “*.example.com” so that all requests to subdomains go to this virtual host.
  2. Setup a rewrite rule for it to redirect all requests to one page, say index.jsp
  3. Write up a JSP page taking the hostname the user came to, searching the redirect URL into a storage by it, i.e. mysql db, and redirecting to it. This script will be the index.jsp script on the wildcard host.

If you use this way, you only need to setup the above once and then add the subdomains in to the storage which appears to be more flexible solution than creating subfolders and modifying Apache configuration.


You have two issues.

First you will need to have the browser of the user recognize where "username.mysite.com" is located. This requires DNS configuration, and usually in the form of a "CNAME wildcard". Note that dyndns.org provides this in their Dynamic DNS Pro package, if you want to have control yourself in the try-out-phase.

The second one is that you will need to tell Tomcat to derive the user from the hostname, and provide a special site for each user. The easiest way to do this, is most likely by having a servlet filter that "tastes" on the hostname in the URL and sets the properties you need for your jsp-pages.


if you are on windows, edit the \windows\system32\drivers\etc\hosts file and add a line like this:

127.0.0.1  username.mysite.com

then when the server is running, you could open the browser and type:

http://username.mysite.com
http://username.mysite.com:8080

or whatever and see the website.

you will need admistrator privileges for this.

if you are on linux, you will need to edit /etc/hosts, and do the same thing.

however, when you are deploying, it is more about configuring the webserver and the dns server. so if you want to get the full experience, you should install a dns server locally and use that as your networks nameserver. this is a more complicated task, and the instructions depend on whether you are on linux or windows, and which webserver you are using.

then you have to write a bit of code in your application to check the server variables to find out which subdomain was used and do the processing based on that.


It looks like you are looking for Virtual Hosts on Tomcat. Then the subdomains need to be configured in Tomcat:

http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html

The web application only controls the URL part starting on the context root. So if there is a web app at "www.mysite.com/", it can handle requests to "www.mysite.com/alice" or "www.mysite.com/bob" but it will not handle requests to "alice.mysite.com/" if this has not been configured in the Tomcat server. This means, that for every new user, Tomcat needs to be configured with the new subdomain. Ii think this can not be done from within the web application.


You could create a wildcard virtual host

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
</VirtualHost>

and then deal with subdomains inside your application code using something like HttpServletRequest#getRequestURL()


I know this is an old question, but I actually came up with an answer that works for Tomcat recently:

First, you need Tomcat behind Apache.

Second, you'll need to create a website definition like this:

<VirtualHost *:80>

    ProxyRequests On
    ProxyVia On
    <Proxy http://example.com>   #FIXDOMAIN
        Order deny,allow
        Allow from all
    </Proxy>

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]   #FIXDOMAIN
    RewriteRule ^(.*)$ http://example.com$1?proxy_domain=%{HTTP_HOST} [P,QSA]    #FIXDOMAIN

    JkMount /* default

    ServerName example.com    #FIXDOMAIN
    ServerAlias *.example.com    #FIXDOMAIN

    <!-- Other normal options go here -->

</VirtualHost>

Third, you need to set up your virtual domain in Tomcat to be example.com.

<Host name="example.com" appBase="webapps-example"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
    <Alias>www.example.com</Alias>
</Host>

This will create a wildcard domain that Apache can process. The mod_rewrite rules will rewrite the URL to the single URL that Tomcat understands. Then, via mod_proxy, the subdomain domain is proxied to the base domain, along with a GET parameter called proxy_domain (which is tacked on top of existing GET parameters) to determine what the original username was.

This is a pretty complicated (and probably not very scalable solution) due to the fact that Apache Tomcat does not support wildcard domains. But it does work!

0

精彩评论

暂无评论...
验证码 换一张
取 消