Migrating servers is a complex process that involves many moving parts. One of the most challenging aspects of server migration is dealing with DNS propagation. When you update your DNS records to point to a new server, it can take several hours for the changes to propagate globally. During this time, some users may still be directed to the old server, which can lead to issues with data consistency, ordering, and revenue loss.
A reverse proxy can help mitigate these issues by allowing you to control the exact moment when the new server starts serving all requests to your domain. In this article, we’ll explore how to set up a reverse proxy using Nginx or Apache, and discuss some additional considerations and alternatives.
What is a Reverse Proxy?
A reverse proxy is a server that receives requests from the internet, obtains the requested resource from the server it’s acting as a proxy for, and then returns that resource to the requester. This is different from a forward proxy, which is typically used to cache frequently requested resources or hide internal IP addresses.
You may have already used a reverse proxy in your setup, for example, by installing Nginx in front of Apache to take advantage of Nginx’s speed and caching capabilities.
How Can a Reverse Proxy Help with DNS Propagation?
A reverse proxy can’t speed up DNS propagation, but it can help mitigate the issues associated with waiting for it to happen. When you’re migrating a server, you can set up the old server as a reverse proxy for the new server. This way, even though some users may still be directed to the old server, the reverse proxy will ensure that all requests are served by the new server.
For example, suppose you’re moving a client’s eCommerce site from an old server to a new server at a different provider. You’ve set up everything on the new server and are ready to switch over the DNS. However, you realize that some users may still be directed to the old server until their local DNS cache is updated. By setting up the old server as a reverse proxy for the new server, you can ensure that all requests are served by the new server, even if some users are still being directed to the old server.
Check Your Host’s Recommendations
Your hosting provider may have specific recommendations and policies regarding reverse proxies. WP Engine, for example, generally doesn’t recommend using reverse proxies. There are certain situations where they support it, but it requires some additional configuration.
WP Engine already utilizes reverse proxy technology in their server setup, with Nginx serving as a traffic director and CDN services distributing static files globally. If you still need to use a reverse proxy, WP Engine recommends forwarding real IP addresses to ensure accurate identification of users and prevent potential security issues.
WP Engine allows two primary proxy configurations: hosting only a subdirectory and hosting both a subdirectory and top-level domain. Each of these has specific configuration instructions.
In addition to your host’s recommendations, there are some other things you should consider before going down this route. Using a reverse proxy can introduce additional latency and resource usage. Make sure to monitor your server’s performance and adjust your configuration accordingly. In addition, the reverse proxy will not proxy database connections, so you’ll need to update your database connection settings to point to the new server.
Using a reverse proxy introduces several security risks. It can become a single point of failure, and if not properly configured, it can expose vulnerabilities. Reverse proxies can store sensitive information like IP addresses and passwords, which can be problematic if managed by a malicious party. Additionally, they are susceptible to HTTP request smuggling attacks and can disrupt operations if they fail. Proper setup and ongoing management are crucial to mitigate these risks effectively.
Setting up the Reverse Proxy
The original version of this article only included instructions for setting up a reverse proxy with Apache. We’ve updated those instructions, and added the process for using Nginx. You can skip to the Apache process here.
Setting up a Reverse Proxy Using Nginx
Create a new file in the /etc/nginx/conf.d/
directory, such as my-proxy.conf
. This file will contain the configuration for your reverse proxy.
sudo nano /etc/nginx/conf.d/my-proxy
In the my-proxy
file, add the following configuration for both HTTP and HTTPS traffic:
server {
listen 80;
server_name mydomain.com www.mydomain.com;
error_log /var/log/nginx/proxy-error.log crit;
access_log /var/log/nginx/proxy-access.log combined;
location / {
proxy_pass http://new-server-ip-address:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 443 ssl;
server_name mydomain.com www.mydomain.com;
error_log /var/log/nginx/proxy-error.log crit;
access_log /var/log/nginx/proxy-access.log combined;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
location / {
proxy_pass https://new-server-ip-address:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Replace mydomain.com
with your site’s domain name, new-server-ip-address
with the IP address of your new server, and update the paths to your SSL certificate and private key files. Finally, restart Nginx to apply the new configuration:
sudo service nginx restart
Nginx Considerations
There are a couple of things you should keep in mind when setting up a reverse proxy with Nginx:
Proxy Caching: Nginx has built-in support for proxy caching, which can help reduce the load on your origin server. You can enable proxy caching by adding the
proxy_cache
directive to your configuration.Buffering: Nginx can buffer responses from the origin server, which can help improve performance. You can enable buffering by adding the
proxy_buffering
directive to your configuration.
Setting up a Reverse Proxy Using Apache
To set up a reverse proxy using Apache, you’ll need to enable the proxy_http
and ssl
modules, and create a new virtual host configuration file. Here’s an example of how to do this on Ubuntu 22.04:
sudo a2enmod proxy proxy_http ssl
sudo nano /etc/apache2/sites-available/my-proxy.conf
In the my-proxy.conf
file, add the following configuration:
<VirtualHost *:443>
ServerName mydomain.com
ServerAlias www.mydomain.com
ErrorLog ${APACHE_LOG_DIR}/proxy-error.log
CustomLog ${APACHE_LOG_DIR}/proxy-access.log combined
SSLEngine on
SSLCertificateFile /path/to/your/certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
ProxyRequests Off
ProxyPass / https://new-server-ip-address:8080/
ProxyPassReverse / https://new-server-ip-address:8080/
ProxySet Header Host $host
ProxySet Header X-Real-IP $remote_addr
ProxySet Header X-Forwarded-For $remote_addr
</VirtualHost>
Replace mydomain.com
with your site’s domain name, new-server-ip-address
with the IP address of your new server, and update the paths to your SSL certificate and private key files.
To enable the new configuration, run the following command:
sudo a2ensite my-proxy
Finally, restart Apache to apply the new configuration:
sudo service apache2 restart
Apache Considerations
There are a few things you should keep in mind when setting up a reverse proxy with Apache:
- SSL Configuration: Make sure to update the
SSLCertificateFile
andSSLCertificateKeyFile
directives to match the paths to your SSL certificate and private key files. - Server Aliases: Update the
ServerName
andServerAlias
directives to match your site’s domain name. - Proxy Settings: Consider using a more secure way to store your SSL certificate and private key files, such as using a password-protected file or a secure keyring.
Wrapping Up
Using a reverse proxy can be a useful tool for mitigating the issues associated with DNS propagation, but it requires careful planning and configuration to ensure that it is implemented correctly. Make sure to look into your host’s recommendations before you begin, and be aware that there are security risks that come with implementing a reverse proxy.
A reverse proxy can be an incredibly powerful tool for routing traffic around and through tough situations, but there’s always more than one way to do it. What are your best tips for migrating servers or creative uses of the reverse proxy? Let us know in the comments!