Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have apache2 running on the port 80 and other application server running on 81.

I would like to redirect traffic only for one specific, selected domain (mydomain.com) to server running on the port 81 and access this website like http://mydomain.com/

I added iptables rule:

iptables -A PREROUTING -t nat -i eth0 -p tcp -d mydomain.com -j REDIRECT --to-port 81

but unfortunately not only this selected domain have been redirected, but also all other websites running on port 80 and apache2 that shouldn't be redirected to port 81.

Is there any solution to achieve only one selected domain redirection to another port?

Any help would be highly appreciated.

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

You can configure it from apache using NameVirtualHost directive and additional ProxyPass and ProxyPassReverse directives provided by mod_proxy module. Here is a basic configuration.

NameVirtualHost *:80

<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www/mydomain
ProxyPass / http://localhost:81/
ProxyPassReverse / http://localhost:81/
</VirtualHost>

<VirtualHost *:80>
ServerName www.mydomain2.com
DocumentRoot /var/www/mydomain2
</VirtualHost>
share|improve this answer
    
Thanks for your answer. I tried that already before. The only problem was that I added something like that: <VirtualHost 172.20.30.40:81> ServerName mydomain.com Redirect / mydomain.com:81 </VirtualHost> because it's liferay portal running on this 81 port and I had a chance access it only as: mydomain.com:81 instead of mydomain.com therefore I gave up with this solution and started thinking about other solutions –  joan Dec 3 '11 at 19:25
    
@joan please check again. I have updated my answer. –  Sachin Divekar Dec 3 '11 at 19:29
add comment

iptables cannot see HTTP host headers.

What you'll want to do is either an HTTP redirect from Apache (telling the client to look at http://mydomain.com:81/) or reverse proxying from your port 80 Apache to the port 81 application (the client's URL won't need to change, but all traffic will be passed through Apache, adding additional overhead).

share|improve this answer
    
You are right. That makes some sense. Thanks for your answer. –  joan Dec 3 '11 at 19:24
add comment

First, the use of DNS names in iptables rules is discouraged. Iptables will lookup the name only once at the beginning and the retrieved IP will be used instead of the name. So, avoid using DNS names in iptables rules.

Second, you can do this using iptables only if you have a different IP address for each domain you want to distinguish (redirect) to another port. Otherwise, you need to use other ways such as: reverse proxy, redirection, etc...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.