How to install WordPress on a different server/IP using Nginx proxy pass

How to install WordPress on a different server/IP using Nginx proxy pass

Recently, I had a client that needed to install a WordPress blog in a “blog” subdirectory, however, the blog needed to live on a different server. I suggested (not thinking about SEO, at all) that perhaps we simply point “blog.example.com” to the other server’s IP address and use the subdomain instead. The client reminded me that Google likes “/blog” example.com/blog better. The main site is hosted on an Nginx box. My thoughts went to Apache mod_proxy which will do me no good here. So, enter Nginx’s proxy_pass. Here’s the code I started with:

As you can see, I’m STILL pushing the “/blog” directory to the “blog.” subdomain where I have installed WordPress in a “/blog” subdirectory. Ok, I’ll say that all again:

  1. Setup DNS for a subdomain of something like “blog.example.com” and point the A record to the new IP
  2. Install WordPress on the new server inside the “/blog” directory.
  3. On your “example.com” server, navigate to the /etc/nginx/sites-enabled/default file (or wherever you have your Nginx config) and add the location code above.

Ok, so, then restart everything and it, at first glance, appears to be working just fine. But, then you notice when visiting /wp-admin everything starts to break! Why?! Well, it took me quite some time to figure out, but, when you hit the wp-admin URL it resolves to a “.php” extension file.  My Nginx config had the following:

Well, the above code will redirect ALL files with the .php extension to the internal workings of PHP on the server in question. INCLUDING those inside of the “/blog” directory. So, regex to the rescue:

… now, all files with the .php extension will still use the server’s PHP, however, it will ignore everything with the .php extension if it’s referring to it inside of the “/blog” subdirectory.  So, now when a user hits http://example.com/blog/[whatever] it will route to the “http://blog.example.com/blog” server using the proxy pass code way at the top of this post.

I sure hope this helps somebody. Let me know! 🙂

Submit a Comment

Your email address will not be published. Required fields are marked *