Skip to content Перейти до змісту


How to redirect traffic to another machine in Linux Як перенаправити трафік на іншій машині в Linux

LinuxWeb Design & Dev

If you have ever handled the migration of a web service or a website from one server to another you know how crazy the experience can be. Якщо ви коли-небудь оброблятися міграції веб-служби або веб-сайт з одного сервера на інший, ви знаєте, як божевільні досвід може бути. However, if you break the process up into clear steps and run constant checks you can make the experience a little easier on yourself. Однак, якщо ви відмовитеся від процесу на чіткі кроки і запустити постійну перевірку можна зробити досвід трохи легше на себе. One of the problems that you might run into towards the end of the migration is the period when you have the website running well on the new location but need to wait for the domain name to be forwarded to the new server. Одна з проблем, з якими ви можете зіткнутися в кінці міграції є періодом, коли у вас є веб-сайт працює добре на новому місці, але потрібно чекати ім'я домену, який буде направлений на новий сервер. you can either shut down your service till the domain is done forwarding, or you can setup your first server to forward all its traffic to the new server. Ви можете вимкнути службу до домен зробити пересилку, або ви можете налаштувати ваш перший сервер для перенаправлення всього трафіку на свій новий сервер. Let's take a look at how you can do that on a Linux machine using IPTables. Давайте подивимося, як ви можете зробити це на машині Linux за допомогою IPTables.

In case you didn't already know, IPtables is a software firewall that ships with most distributions of Linux. У випадку, якщо ви вже не знаєте, IPtables це програмний брандмауер, який постачається з більшістю дистрибутивів про Linux. It is an extremely useful software and can be used for a lot more than just as a firewall. Це дуже корисна програма, і може бути використана для набагато більше, ніж просто як брандмауер. In this exercise we will configure IPTables on a Linux server to redirect all the traffic coming on port 80, (which is the default web server port), to a server with the IP 122.164.34.240 . У цій вправі ми настроїмо IPTables на сервері Linux для переадресації всього трафіку, що надходить на порт 80, (який є сервером за замовчуванням веб-порт), на сервер з IP 122.164.34.240. The first step is to set your Linux box to allow this kind of forwarding to take place. Першим кроком буде встановити Linux вікна, щоб такого роду експедиторських мати місце. Open a terminal window, log in as root user and run the following command: Відкрийте вікно терміналу, увійдіть в систему як користувач корінь і запустіть наступну команду:

# echo 1 >/proc/sys/net/ipv4/ip_forward Ехо # 1> / proc/sys/net/ipv4/ip_forward

The next step is to tell IPTables to redirect the traffic to the new server: Наступним кроком буде сказати IPTables для перенаправлення трафіку на новий сервер:

# iptables -t nat -D PREROUTING -p tcp –dport 80 -j DNAT –to-destination 122.164.34.240 # IPTABLES-т фіз-D INPUT-р TCP-dport 80-й DNAT до призначення 122.164.34.240

Here's where the IPTables magic happens. Ось де магія IPTables відбувається. With the third and final step we tell IPTables to rewrite the origin of connections to the new server's port 80 to appear to come from the old server. У третьому і останньому кроці ми говоримо IPTables переписати походження підключень до порту новий сервер на 80-мабуть, зі старого сервера.

# iptables -t nat -D POSTROUTING -p tcp -d 122.164.34.240 –dport 80 -j MASQUERADE # IPTABLES-т фіз-D POSTROUTING-P-D TCP 122.164.34.240-dport 80-й MASQUERADE

The final step is required because if we don't tell the web server of the new server that the connections are coming from the client machines, it would think that they are originating from the old server. Остаточний крок необхідний, тому що якщо ми не розповідаємо веб-сервера на новий сервер, що сполуки надходять з клієнтських машин, вона буде думати, що вони поставлені зі старого сервера.

Posted in Опубліковано в Linux Linux , , Web Design & Dev Web Design & Dev . .

Get Simple Help tutorials just like this one in your email inbox every day - for free! Отримати довідки Прості підручників, як це у вашому поштовому ящику щодня - безкоштовно! Just enter your email address below: Просто введіть свою адресу електронної пошти нижче:

You can always opt out of this email subscription at any time. Ви завжди можете відмовитися від підписки це повідомлення в будь-який час.

2 Responses 2 Відповіді

Stay in touch with the conversation, subscribe to the Залишайтеся на зв'язку при розмові, підпишіться на RSS feed for comments on this post RSS-канал для коментарів на цю посаду . .

  1. cosco says COSCO говорить

    Thanks, I googled a lot before finding your post, without success. Спасибо, я Googled багато чого, перш ніж знайшла своє повідомлення, без особливого успіху.
    That does exactly what I want. Це робить саме те, чого я хочу.
    However, there is a typo : Однак, є помилка:
    “iptables -t nat -D PREROUTING” should read “iptables -t nat -A PREROUTING” "IPTABLES-т фіз-D PREROUTING" слід читати "IPTABLES-т фіз-PREROUTING"

  2. David Madl Девід Мадлен says говорить

    Many thanks, works perfectly well with -A instead of -D (which is for deleting the rules). Велике спасибі, відмінно працює з-а-D (який призначений для видалення правил).



Some HTML is OK Деякі HTML нормально

or, reply to this post via або відповісти на цей пост через trackback Архів . .