Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ( to server running on the port 81 and access this website like

I added iptables rule:

iptables -A PREROUTING -t nat -i eth0 -p tcp -d -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
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>
DocumentRoot /var/www/mydomain
ProxyPass / http://localhost:81/
ProxyPassReverse / http://localhost:81/

<VirtualHost *:80>
DocumentRoot /var/www/mydomain2
share|improve this answer
Thanks for your answer. I tried that already before. The only problem was that I added something like that: <VirtualHost> ServerName Redirect / </VirtualHost> because it's liferay portal running on this 81 port and I had a chance access it only as: instead of 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

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 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

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

Your Answer


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.