How to use nginx to proxy a subdirectory to a subdomain

It’s common to host a wordpress blog on a subdomain like on separate servers from the main site for various reasons.

For SEO purpose(Google thinks subdomain is a separate site), you may want to use for your blog but keeping the blog installation on original server. It’s simply to do this using a proxy server.

For example: is on server A. is on server B.
We have a proxy server C.

Now, we need setup the proxy for main domain on server C.

The nginx site conf will look like this:

# proxy subdirectory to subdomain
location /blog/ {
    proxy_set_header Host;
    # strip /blog/ from the path
    rewrite /blog/(.*) /$1 break;

# proxy everything else to main domain server
location / {
    # pass everything to server
    proxy_pass http://server_A_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 http;


Test conf and restart nginx

nginx -t && systemctl restart nginx

Next, change main domain A record to the proxy server. Once DNS is propagated, you will need to install the SSL for main domain so that you can use to go to your blog.

certbot --nginx -d

The only problem is that the links are still using the subdomain address You can simple add following two lines in wp-config.php file to replace all subdomain links to the proxy address “”

define('WP_HOME', '');
define('WP-SITEURL', '');

Now, when you go to, you will see what you have on
All the blog posts/pages will also be translated to the proxy address like it’s hosting on the same server as the main domain.