How To Set Up a Reverse Proxy for WordPress
Reverse Proxy for WordPress
Collapse
Unconfigured Ad Widget
Collapse
X
-
As a WordPress administrator, you may encounter scenarios where setting up a reverse proxy server becomes necessary. Thankfully, the process is relatively straightforward, and there are various software packages available for this purpose. This article offers a concise guide to configuring a reverse proxy server for your WordPress site using NGINX.
What is a reverse proxy?
A reverse proxy server retrieves resources from one or more servers on behalf of a client and delivers them to the client without revealing their origin to the proxy server.
Reverse proxies play a crucial role in concealing the presence and traits of the originating server. This functionality provides an additional layer of security, performance optimization, and enhanced reliability for web applications. It acts as a shield against malicious requests and effectively manages substantial traffic loads.
How to set up Nginx as a reverse proxy?
For example, we have set up the main site using the domain name demo.com, while the WordPress site, acting as the proxy server, is installed under the subdomain blog.domain.com. Both sites are hosted on an Apache web server running on Ubuntu 18.04. To enhance the setup, we will install and configure Nginx as a reverse proxy on the primary server.
1. Install Nginx
Access your server's terminal through SSH. Use the `apt-get` command to update the package list for your distribution and install Nginx on your web server.
sudo apt update
sudo apt install nginx
2. Configure Nginx to Proxy Requests
Next, configure Nginx to act as a proxy for domains hosted on Apache. Create a new virtual host file for this purpose.
sudo nano /etc/nginx/sites-available/demo.com.conf
Afterwards, configure Nginx directives to route requests to Apache by incorporating the following server {...} and location blocks:
server {
listen 80;
server_name demo.com www.demo.com;
index index.php;
root /var/www/demo.com/public # fallback for index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}location /blog {
proxy_pass http://blog.domain.com;proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
# Proxy headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
In the provided code, I am establishing a subdirectory demo.com/blog link, which will be handled by the Apache server. Make sure to use the public IP address (or URL) of your proxied website in the proxy_pass directive. In my scenario, the proxied website is hosted on the blog.domain.com subdomain.
3. Save the Virtual Host File Created
Then, enable the newly configured virtual host by creating symbolic links for the files named demo.com.conf in both the /etc/nginx/sites-available and /etc/nginx/sites-enabled directories.
sudo ln -s /etc/nginx/sites-available/demo.com.conf /etc/nginx/sites-enabled/demo.com.conf
4. Test nginx for errors
sudo nginx -t
If there are no errors, reload Nginx to apply the changes.
sudo systemctl reload nginx
You have successfully configured Nginx to operate as a reverse proxy. To verify, you can use the phpinfo() function to inspect the loaded PHP variables when accessing your proxied site.
-
Comment