Home > Cpanel/WHM, Linux Command Line > Redirect HTTP traffic to another IP using iptables

Redirect HTTP traffic to another IP using iptables

 

 

SITUATION: After migrating a website to a new server, I want to redirect all traffic coming to the old server’s http port(during the TTL change period) to the webserver running in new server with a different ip .

 

DETAILS:

Service 					:	Apache(port 80 and 443)
Interface name in old server(this is a vps)	:	venet0    
Destination ip					:  	98.59.254.163

 

 

SOLUTION:

 

Use iptables nat to redirect http and https traffic to another server ip and port. Execute the following commands in the source server.

 

iptables -t nat -A PREROUTING -i venet0  -p tcp --dport 80 -m conntrack --ctstate NEW -j DNAT --to 98.59.254.163:80
iptables -t nat -A PREROUTING -i venet0  -p tcp --dport 443 -m conntrack --ctstate NEW -j DNAT --to 98.59.254.163:443
iptables -t nat -A PREROUTING -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE

 

Advertisements
  1. May 3, 2013 at 12:54 am

    It’s awesome for me to have a website, which is useful designed for my knowledge. thanks admin

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: