Nginx: How to point the root to a Ghost blog located in a subdirectory

ghost-blognginxrewriteurl

I have installed Ghost inside a blog/ directory in my site's root. The site works as domain.com/blog/ and serves static files from public_html/ directory (including index.html). I would like to have the index point to domain.com/blog/ without redirecting the URL (i.e. http://www.domain.com would serve the blog homepage from http://www.domain.com/blog/).

My config file:

 server {
   server_name domain.com www.domain.com;

   location / {
     root /var/www/domain.com/public_html;
     access_log /var/www/domain.com/logs/access.log;
     error_log /var/www/domain.com/logs/error.log;
     index index.html index.htm;
   }

   location ^~ /blog {
     proxy_set_header  X-Real-IP $remote_addr;
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header  Host      $http_host;
     proxy_set_header  X-NginX-Proxy true;

     proxy_pass        http://127.0.0.1:2368;
     proxy_redirect    off;
   }
 }

Best Answer

I figured it out. Just needed to add a rewrite rule:

location / {
  root /var/www/domain.com/public_html;
  access_log /var/www/domain.com/logs/access.log;
  error_log /var/www/domain.com/logs/error.log;
  index index.html index.htm;
  rewrite ^/$ /blog/ last;
}