keepcalculator

06 Numbers and Powers

How to Proxy Nginx to WordPress on Another Server

Card No. 06·71758 · Updated · Standard formula, shown below

This guide explains how to configure Nginx as a reverse proxy to serve a WordPress site that runs on a separate server with PHP. You’ll end up with a setup where visitors hit the Nginx server, and it forwards requests to the backend WordPress server, while keeping the same domain in the browser. This is useful for load balancing, centralizing SSL, or hiding the backend server’s IP. Before starting, you need SSH access to both servers, a working WordPress installation on the backend, and Nginx installed on the front server.

What You’ll Need

  • Two Linux servers (e.g., Ubuntu, Debian, CentOS) with root or sudo access.
  • Nginx installed on the front server (the one that will accept client requests).
  • A working WordPress site on the backend server, already accessible via its IP or a temporary domain.
  • PHP-FPM (or similar) running on the backend server, configured to serve WordPress.
  • A domain name pointing to the front server’s public IP (for production use).
  • Basic familiarity with the command line and editing files.

Step-by-Step

  1. Identify the backend server’s IP and WordPress path. Log into the backend server and confirm WordPress is working by accessing it via http:// or a temporary domain. Note the document root (e.g., /var/www/html) and the port PHP-FPM listens on (commonly 9000 or a Unix socket like unix:/run/php/php8.1-fpm.sock).
  2. Configure WordPress to use the front server’s domain. On the backend server, edit wp-config.php and add or modify these lines (replace example.com with your actual domain):
    define('WP_HOME','http://example.com');
    define('WP_SITEURL','http://example.com');
    This prevents WordPress from redirecting to the backend IP. If you can’t edit the file, you can also set it via the database or use a plugin, but the file approach is most reliable.
  3. Install Nginx on the front server. If not already installed, run sudo apt update && sudo apt install nginx (Debian/Ubuntu) or sudo yum install nginx (RHEL/CentOS). Start and enable it: sudo systemctl enable --now nginx.
  4. Create an Nginx server block for your domain. On the front server, create a new config file: sudo nano /etc/nginx/sites-available/example.com (or in /etc/nginx/conf.d/ on some systems).
  5. Write the reverse proxy configuration. Use the following as a starting point, replacing example.com and BACKEND_IP with your actual values:
    server {
        listen 80;
        server_name example.com;
    
        location / {
            proxy_pass http://BACKEND_IP;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    

    If the backend uses a different port, specify it in proxy_pass (e.g., http://BACKEND_IP:8080).

  6. Enable the site and test the config. Create a symlink: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ (if using sites-enabled). Then test the configuration: sudo nginx -t. If it reports no errors, reload Nginx: sudo systemctl reload nginx.
  7. Test the proxy. From your local machine, visit http://example.com (or the front server’s IP if the domain isn’t set up yet). You should see the WordPress site. If you see the backend IP in the address bar or get a redirect loop, proceed to troubleshooting.
  8. Fix WordPress redirects and asset URLs. If you didn’t set WP_HOME and WP_SITEURL, WordPress might redirect to the backend IP. Also, the site URL might be hardcoded in the database. Use a plugin like “Better Search Replace” or run SQL queries to replace the old URL with the new domain. After that, log into wp-admin and go to Settings > General to confirm the URLs.
  9. Handle HTTPS (optional but recommended). If you have SSL certificates, configure Nginx to listen on 443 and add the SSL directives. Also, set proxy_set_header X-Forwarded-Proto https; so WordPress knows it’s secure. If you use a service like Let’s Encrypt, run sudo certbot --nginx -d example.com after the proxy is working.
  10. Optimize for performance (optional). Add caching headers, enable gzip, and consider using Nginx’s proxy_cache for static assets. For WordPress, you can also install a caching plugin that works behind a proxy.

Common Mistakes

  • Forgetting to set WP_HOME and WP_SITEURL: This is the most common cause of redirect loops or links pointing to the backend IP.
  • Not forwarding the Host header: Without proxy_set_header Host $host, the backend server may serve the wrong virtual host.
  • Blocking the front server’s IP: If the backend has a firewall (like UFW), you must allow traffic from the front server’s IP to the web server port (80/443).
  • Using the wrong PHP-FPM socket path: In the backend’s Nginx config, ensure the fastcgi_pass directive matches the actual PHP-FPM socket or port.
  • Not updating the database after changing domains: Even with WP_HOME, some URLs (like in post content) may still have the old domain.

Tips

  • Use a dedicated Nginx config file per site to avoid confusion.
  • If the backend server is on a private network, use its private IP for proxy_pass to avoid exposing it.
  • Add proxy_read_timeout 300s; if you have large uploads or long-running PHP scripts.
  • For WordPress multisite, you’ll need to adjust the server_name and possibly use wildcard domains.
  • Test with curl -I http://example.com to see response headers and detect redirects.

Troubleshooting

  • Redirect loop (too many redirects): This usually means WordPress is redirecting between the front and backend URLs. Double-check WP_HOME and WP_SITEURL. Also, ensure proxy_set_header Host $host; is present. If using HTTPS, set X-Forwarded-Proto correctly.
  • 404 errors on pages other than the homepage: This indicates the proxy_pass is not passing the full URI. Make sure you don’t have a trailing slash in proxy_pass (e.g., http://BACKEND_IP not http://BACKEND_IP/). Also, check that the backend’s Nginx has a proper try_files directive for pretty permalinks.
  • 502 Bad Gateway: The front server can’t reach the backend. Check that the backend is running, the IP/port is correct, and firewalls allow the connection. Test with curl http://BACKEND_IP from the front server.
  • Mixed content warnings (HTTP/HTTPS): If you enable SSL on the front but not the backend, or vice versa, update WP_HOME to use HTTPS and set X-Forwarded-Proto to https.
  • Admin login not working: This is often due to cookies not being set correctly. Ensure the Host header is forwarded and that the backend’s PHP session cookie domain matches the front domain.

Frequently Asked Questions

Can I proxy to a WordPress site that already has its own domain?

Yes, but you’ll need to keep that domain as the canonical one or change it. If you want to keep the backend’s domain, you can proxy to it, but the front domain will just be an alias. For a seamless experience, it’s best to make the front domain the primary one.

Do I need to install PHP on the front server?

No, the front server only needs Nginx. PHP runs on the backend server. The proxy just forwards HTTP requests, so no PHP is required on the front.

Will this work with WordPress multisite?

Yes, but you’ll need to configure the server_name to match the multisite domains and possibly use wildcard DNS. Also, ensure the Host header is passed correctly.

How do I handle SSL certificates?

You can install SSL on the front server and use HTTPS to clients. The backend can be HTTP, but you must set X-Forwarded-Proto to https so WordPress knows the original request was secure. Or you can also install SSL on the backend, but that’s optional.

Can I use this to load balance between multiple WordPress servers?

Yes, you can define multiple upstream blocks in Nginx and use proxy_pass http://backend_pool; to distribute traffic. However, WordPress may need a shared database and session handling for full load balancing.

What if the backend server is behind a NAT or firewall?

Make sure the front server can reach the backend’s IP/port. You may need to open a port on the backend’s firewall specifically for the front server’s IP. Avoid exposing the backend to the public internet if possible.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →