Configure Nginx to Serve Ghost and Non-Ghost Files

I set up this Ghost blog on DigitalOcean with the help of this tutorial. I wanted to be able to serve non-Ghost files hosted on the same DigitalOcean droplet as my blog for two reasons:

  1. I had an existing portfolio website I wanted to host on the same server and link to from the blog with the URL stephsharp.me/portfolio

  2. I wanted to be able to upload static files and link to them in my blog posts with a URL like stephsharp.me/files/file.zip. (See this discussion and this discussion on the Ghost support forum on this topic.)

Nginx config for Ghost

After following the tutorial to install Ghost, my Nginx config looked like this:

server {
    listen       80 default_server;
    server_name  stephsharp.me;
    root         /home/ghost/ghost;

    location / {
        proxy_pass http://localhost:2368/;
        proxy_set_header Host $host;
        proxy_buffering off;
    }
}

The root directory is /home/ghost/ghost, and I have just the one location block to serve Ghost content.

Nginx config to serve non-Ghost files

I then set up my preferred directory structure as follows:

|-- home
    |-- files
    |-- ghost
    |-- portfolio

To serve up non-Ghost files in the 'files' and 'portfolio' directories, I altered my Nginx config to have multiple location blocks. It has a default root directive outside the location blocks, and a different root directive within the location block serving Ghost.

I've also used try_files to provide appropriate fallbacks in the /files and /portfolio location blocks.

server {
    listen          80 default_server;
    server_name     stephsharp.me;
    root            /home;

    location / {
        root        /home/ghost/ghost;

        proxy_pass          http://localhost:2368/;
        proxy_set_header    Host $host;
        proxy_buffering     off;
    }

    location /files {
        try_files   $uri $uri/ =404;
    }

    location /portfolio {
        try_files   $uri $uri/ /portfolio/index.html =404;
    }
}

I put this config in a separate file on the server at /etc/nginx/conf.d/blog.conf and have this line in /etc/nginx/nginx.conf:

include /etc/nginx/conf.d/*.conf;

I could have just put the server block into the existing nginx.conf file, but I decided to separate it out into its own config file.

Side note: Remember to restart Nginx with nginx -s reload when you make changes to the config files.