Wednesday, January 19, 2011

Apache redirect based on hostname

I have multiple hostnames resolving to a single machine, e.g.:

  • build.mydomain.com
  • www.mydomain.com
  • jira.mydomain.com
  • mydomain.com

Is it possible to setup apache in order to redirect requests to each different hostname?

e.g:

  • build.mydomain.com -> build.mydomain.com:8111,
  • www.mydomain.com -> www.mydomain.com:8080
  • mydomain.com -> www.mydomain.com:8080

The DNS records are setup to all point to the same machine, i just want to redirect to the right port given the hostname.

Cheers,

Edit:

Machine is debian w/ Apache2

Edit2:

<VirtualHost *:80>
    ServerName www.mydomain.com
    ServerAlias mydomain.com
    redirect 301 / http://www.mydomain.com:8080/
</VirtualHost>

<VirtualHost *:80>
    ServerName build.mydomain.com
    redirect 301 / http://build.mydomain.com:8111/
</VirtualHost>
  • Are you running multiple instances of Apache, each listening on a different port? Why? Virtual hosts will take care of everything.

    http://httpd.apache.org/docs/2.2/vhosts/name-based.html

    slappybag : No, single instance of apache that I wanted to use to redirect requests. Got multiple instances of tomcat running, e.g. TeamCity on 8111, tomcat on 8080 and JIRA on 8081. I wanted to redirect to the respective port given the hostname.
    MidnighToker : how about mod-rewrite running on the apache:80 instance? possibly even apache as proxy but that sounds like far too much effort for a redirect.
    slappybag : I have it half working, using VirtualHost directive with a redirect 301 to the same hostname and different port. This should do for now. Cheers,
    From ynguldyn
  • Actually, I doubt using redirect is winning strategy for what you're trying to do. I'd recommend using mod_proxy to create a reverse proxy which will hide the way you've built your system.

    And if I had the choice, I would use something more lightweight and more convenient to configure, like Perlbal.

    From af

0 comments:

Post a Comment