How to Redirect Traffic from One NGINX Server to Another During DNS Propagation


2 views

When migrating websites between servers, DNS propagation delays can cause headaches. Here's a technical solution I implemented when moving subdomain.site.ru from Server A (old) to Server B (new) while waiting for DNS to fully propagate globally.

The most reliable approach is to modify the NGINX configuration on Server A to proxy requests to Server B:

server {
    listen 80;
    server_name subdomain.site.ru;
    
    location / {
        proxy_pass http://server-b-ip-address;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

For a simpler temporary solution, you can use a 301 redirect:

server {
    listen 80;
    server_name subdomain.site.ru;
    return 301 http://subdomain.site.ru$request_uri;
}

If you're using SSL, ensure you maintain security during the transition:

server {
    listen 443 ssl;
    server_name subdomain.site.ru;
    
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    
    location / {
        proxy_pass https://server-b-ip-address;
        proxy_ssl_server_name on;
        proxy_set_header Host $host;
    }
}

Always verify your changes:

sudo nginx -t
sudo systemctl restart nginx

Use curl to test from different locations:

curl -I http://subdomain.site.ru
curl -v https://subdomain.site.ru

Keep an eye on both servers during the transition period:

tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

When migrating websites between servers, DNS propagation delays can leave users seeing outdated content for hours or even days. Here's how to implement immediate redirects at the NGINX level while waiting for DNS to fully propagate.

On your old server (Server A), modify the NGINX configuration for the subdomain to return 301 redirects:

server {
    listen 80;
    server_name subdomain.site.ru;
    return 301 http://subdomain.site.ru$request_uri;
}

For more complex scenarios where you need to maintain query strings:

server {
    listen 80;
    server_name subdomain.site.ru;
    if ($args) {
        return 301 http://new.subdomain.site.ru$request_uri?$args;
    }
    return 301 http://new.subdomain.site.ru$request_uri;
}

If your sites use HTTPS, you'll want to properly handle SSL certificates during the transition:

server {
    listen 443 ssl;
    server_name subdomain.site.ru;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    return 301 https://new.subdomain.site.ru$request_uri;
}

Use curl to verify your redirects are working correctly:

curl -I http://subdomain.site.ru
HTTP/1.1 301 Moved Permanently
Server: nginx
Location: http://new.subdomain.site.ru/

For testing purposes, you might want to redirect only specific IP ranges:

geo $redirect {
    default 0;
    192.168.1.0/24 1; # Office network
}

server {
    listen 80;
    server_name subdomain.site.ru;
    
    if ($redirect) {
        return 301 http://new.subdomain.site.ru$request_uri;
    }
    
    # Original configuration
    root /var/www/subdomain/public;
    passenger_enabled on;
}

Remember that each redirect adds an additional HTTP request. For optimal performance:

  • Keep redirect chains as short as possible
  • Use absolute URLs in Location headers
  • Set proper cache headers for the redirect responses