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
- 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 (commonly9000or a Unix socket likeunix:/run/php/php8.1-fpm.sock). - Configure WordPress to use the front server’s domain. On the backend server, edit
wp-config.phpand add or modify these lines (replaceexample.comwith 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. - Install Nginx on the front server. If not already installed, run
sudo apt update && sudo apt install nginx(Debian/Ubuntu) orsudo yum install nginx(RHEL/CentOS). Start and enable it:sudo systemctl enable --now nginx. - 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). - Write the reverse proxy configuration. Use the following as a starting point, replacing
example.comandBACKEND_IPwith 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). - 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. - 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. - Fix WordPress redirects and asset URLs. If you didn’t set
WP_HOMEandWP_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. - 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, runsudo certbot --nginx -d example.comafter the proxy is working. - Optimize for performance (optional). Add caching headers, enable gzip, and consider using Nginx’s
proxy_cachefor static assets. For WordPress, you can also install a caching plugin that works behind a proxy.
Common Mistakes
- Forgetting to set
WP_HOMEandWP_SITEURL: This is the most common cause of redirect loops or links pointing to the backend IP. - Not forwarding the
Hostheader: Withoutproxy_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_passdirective 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_passto 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_nameand possibly use wildcard domains. - Test with
curl -I http://example.comto 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_HOMEandWP_SITEURL. Also, ensureproxy_set_header Host $host;is present. If using HTTPS, setX-Forwarded-Protocorrectly. - 404 errors on pages other than the homepage: This indicates the
proxy_passis not passing the full URI. Make sure you don’t have a trailing slash inproxy_pass(e.g.,http://BACKEND_IPnothttp://BACKEND_IP/). Also, check that the backend’s Nginx has a propertry_filesdirective 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_IPfrom the front server. - Mixed content warnings (HTTP/HTTPS): If you enable SSL on the front but not the backend, or vice versa, update
WP_HOMEto use HTTPS and setX-Forwarded-Prototohttps. - Admin login not working: This is often due to cookies not being set correctly. Ensure the
Hostheader 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
- Samsung Unpacked August 5: 5 Devices Teased?
- Google Workspace Accounts Created Bypassing?
- Recover Deleted Screenshots on Mac: Full Guide
- Go vs Node.js: Cloudflare API Request Data
- ROG Phone 3 Certified Specs Leak: What’s Confirmed
- Clear Cache on a Philips Smart TV: Right Menu