This guide explains how to set up Nginx as a reverse proxy in front of a WordPress site. This setup lets Nginx handle client requests, SSL termination, and static file caching while passing PHP requests to a backend server (like Apache or PHP-FPM). It improves performance, simplifies SSL management, and allows you to run WordPress on a different port or host. You’ll need a Linux server with Nginx installed, a running WordPress site (on Apache, PHP-FPM, or another backend), and sudo access.
What You’ll Need
- A server running Linux (Ubuntu, Debian, CentOS, etc.) with Nginx installed.
- A WordPress site already running on a backend web server (Apache, nginx itself, or PHP-FPM). The backend can be on the same machine or a different one.
- Root or sudo access to edit Nginx configuration files.
- A domain name pointing to your server’s public IP (if you want to use a domain).
- Optional: An SSL certificate (e.g., Let’s Encrypt) for HTTPS.
Step-by-Step
- Identify your backend WordPress URL. Determine the address where WordPress is currently running. For example, if WordPress is on Apache on the same server, it might be
http://localhost:8080orhttp://127.0.0.1:8080. If it’s on a different server, use that server’s IP or hostname. - Back up your current Nginx configuration. Before making changes, copy the main config file:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup. Also back up any site config files in/etc/nginx/sites-available/. - Create a new Nginx server block (virtual host). Create a file in
/etc/nginx/sites-available/, e.g.,sudo nano /etc/nginx/sites-available/wordpress-reverse-proxy. This will contain the reverse proxy configuration. - Write the reverse proxy configuration. Here’s a basic example that listens on port 80 and proxies requests to a backend on port 8080. Replace
example.comwith your domain or IP, and adjust theproxy_passURL to match your backend.server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; 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 you have an SSL certificate, add a
listen 443 ssl;block and include the certificate paths. Also consider adding a redirect from HTTP to HTTPS. - Enable the site and test configuration. Create a symlink to enable the site:
sudo ln -s /etc/nginx/sites-available/wordpress-reverse-proxy /etc/nginx/sites-enabled/. Then test the configuration withsudo nginx -t. If there are errors, fix them before proceeding. - Reload Nginx. Apply the changes by running
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). - Update WordPress settings (if needed). If your WordPress site is configured to use the backend URL directly, you may need to update the
siteurlandhomeoptions in the WordPress database. You can do this via the WordPress admin dashboard (Settings > General) or by running SQL commands. For example, if the backend washttp://localhost:8080and the public URL ishttp://example.com, update the options accordingly. Also, ensure that the backend server is configured to trust the proxy headers (X-Forwarded-For, etc.) so that client IPs are logged correctly. - Test the reverse proxy. Visit your domain or IP in a web browser. You should see your WordPress site. Check that links work, images load, and the admin area is accessible. Use
curl -I http://example.comto see response headers and confirm that the server is Nginx.
Common Mistakes
- Incorrect proxy_pass URL: Using the wrong port or path in
proxy_passis a common error. Ensure it matches exactly where your WordPress backend is listening. - Missing proxy headers: Without the
X-Forwarded-ForandX-Forwarded-Protoheaders, WordPress may not detect the client’s real IP or whether the connection is HTTPS, which can break some plugins or cause redirect loops. - Not updating WordPress URLs: If the backend URL is still hardcoded in the database, the site may redirect to the backend URL instead of the public one.
- Forgetting to enable the site: If you don’t create the symlink in
sites-enabled, Nginx won’t load the configuration and you’ll see the default page. - SSL certificate misconfiguration: If you’re using SSL, ensure the certificate paths are correct and that you have a redirect from HTTP to HTTPS to avoid mixed content warnings.
Tips
- Use a unix socket for PHP-FPM: If your backend is PHP-FPM, consider using a unix socket (e.g.,
unix:/var/run/php/php7.4-fpm.sock) instead of a TCP port for better performance. - Cache static assets: Add a
locationblock for static files (CSS, JS, images) to serve them directly from Nginx, bypassing the backend. For example:location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { expires 30d; add_header Cache-Control "public, no-transform"; proxy_pass http://127.0.0.1:8080; } - Enable gzip compression: Turn on gzip in the main Nginx config to reduce bandwidth usage.
- Set proper timeouts: Adjust
proxy_read_timeoutandproxy_connect_timeoutto avoid timeouts for long-running requests (e.g., media uploads). - Use a separate location for wp-admin: If you experience issues with the admin area, you can add a specific location block for
/wp-adminwith different proxy settings.
Troubleshooting
Site returns 502 Bad Gateway: This usually means Nginx cannot connect to the backend. Check that the backend is running and the proxy_pass URL is correct. Use sudo tail -f /var/log/nginx/error.log to see the exact error.
Redirect loop (too many redirects): This often happens when WordPress is configured to use HTTPS but Nginx is not passing the correct X-Forwarded-Proto header, or when the backend is redirecting to a different URL. Ensure the header is set and that WordPress’s siteurl and home match the public URL.
Login page not working: If you can load the site but the login page redirects to the backend URL, the siteurl and home options are likely incorrect. Update them via the database or by adding define('WP_HOME', 'http://example.com'); define('WP_SITEURL', 'http://example.com'); to wp-config.php.
Client IPs showing as 127.0.0.1: This means the X-Forwarded-For header is not being passed or the backend is not using it. Make sure the header is set in Nginx and that your backend (Apache or PHP-FPM) is configured to use it (e.g., with mod_remoteip for Apache).
Static files not loading: If CSS/JS are broken, check that the proxy is correctly passing requests for those files. If you added a static file location, ensure it doesn’t conflict with the main location / block.
Frequently Asked Questions
Can I use Nginx as a reverse proxy for WordPress on Apache?
Yes, Nginx can proxy to any backend that speaks HTTP, including Apache. Just set proxy_pass to Apache’s address (e.g., http://127.0.0.1:8080).
Do I need to change my WordPress configuration?
You may need to update the siteurl and home options if they are set to the backend URL. Also, ensure your backend is configured to trust proxy headers.
Will this affect my WordPress permalinks?
No, permalinks are handled by WordPress and the backend, not Nginx. As long as the proxy passes all requests, permalinks will work.
How do I enable HTTPS with a reverse proxy?
Add a listen 443 ssl; block to your Nginx server, specify your certificate and key, and consider redirecting HTTP to HTTPS. Also, set X-Forwarded-Proto $scheme so WordPress knows it’s HTTPS.
Can I run multiple WordPress sites behind one Nginx?
Yes, you can create multiple server blocks, each with its own server_name and proxy_pass to different backends.
What if my backend uses a different path (e.g., /wordpress)?
You can adjust the proxy_pass to include the path, e.g., proxy_pass http://127.0.0.1:8080/wordpress;. However, be careful with path rewriting; you may need to use proxy_redirect to handle redirects correctly.
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 Smart Speaker Rollable Display: Facts
- HP 9730e: Wide Prints, Not A3 Scans
- What 8888 Does When You Use It as DNS Server
- Credential Manager: Seamless Sign-In Explained
- API Governance for Public Data: What Growing Portfolios Need
- Enterprise AI Hidden Cost: Data Architecture ROI