Nginx – Hosting a WordPress Blog using Nginx as sub-directory

nginxWordpress

I have an existing meteor application and am using nginx as my server, lets say at example.com

I want to add a wordpress blog, which should be accessed at example.com/blog

I also preferably want my blog to exist on it's own droplet (server) but if it needs to be on the same droplet (server) then it's ok.

How do I do this?

I have been able to get it to work with a different server block with a different server_name at blog.example.com. I also have gotten static files to be accessible under a sub directory at example.com/blog/index.html. This where everything exists on the same droplet (server).

But I can't get a wordpress application be accessible through example.com/blog.

EDIT:

What I ended up doing is creating a new server instance(droplet), configured it with LEMP stack and NGINX. It works if I try to access the IP.

Now, on my actual server, added a location block

location /blog {
    rewrite ^/blog/(.*)$ /$1 break;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://wordpress-server-ip;
    proxy_redirect off;
}

My new problem is, that my blog is not able to load the .css files in chrome because, my proxy server is configured to https and wordpress loads the files over http. You can check this out at https://juerix.com/blog.

So I went into my wp-config file and changed the site and wordpress url https://juerix.com/blog. Didn't help.

Best Answer

This took me a while to work out. The key is in the try_files line.

# Default location to serve
location / {
  # If the file can't be found try adding a slash on the end - it might be
  # a directory the client is looking for. Then try the Wordpress blog URL
  # this might send a few requests to PHP that don't need to go that way
  try_files $uri $uri/ /blog/index.php?$args;

}

Here's another bit I found useful

# Add trailing slash to */wp-admin requests.
rewrite /blog/wp-admin$ $scheme://$host$uri/ permanent;

Here's another location block I used, just as an example

# Rate limit wp-login.php to help prevent brute force attacks
location = /blog/wp-login.php {
  # Next line applies the rate limit defined above
  limit_req zone=login burst=3;
  fastcgi_keep_conn on;

  fastcgi_pass php56-fpm;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  more_clear_headers "Cache-Control";
   more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires";

  # No caching
  more_clear_headers "Cache-Control";
  add_header Cache-Control "private, max-age=0, no-cache, no-store";
  more_clear_headers "Expires";
}