nginx subdir config with ghost

May 29, 2017 777 views
Nginx Ghost One-Click Install Apps Ubuntu 16.04

Hi, I recently made a Digital Ocean on-click Ghost app droplet and it works great. I'm wanting to add a subdirectory to my nginx config but the configs I've been doing give 404s or 500s. Any tips are greatly appreciated, and I'm curious if the ghost express server needs to be tweaked as well?

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# tail -n 20 /var/log/nginx/error.log
2017/05/28 23:16:14 [error] 15467#15467: *2153 "/var/www/terrarium/terrarium/index.html" is not found (2: No such file or directory), client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:29:07 [error] 25608#25608: *1 "/var/www/terrarium/terrarium/index.html" is not found (2: No such file or directory), client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:29:08 [error] 25608#25608: *1 "/var/www/terrarium/terrarium/index.html" is not found (2: No such file or directory), client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:30:17 [error] 25655#25655: *1 "/var/www/terrarium/terrarium/index.html" is not found (2: No such file or directory), client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:35:47 [error] 25736#25736: *3 rewrite or internal redirection cycle while internally redirecting to "/terrarium/index.html", client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:35:59 [error] 25736#25736: *4 rewrite or internal redirection cycle while internally redirecting to "/terrarium/index.html", client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/index.html HTTP/1.1", host: "xxx.com"
2017/05/28 23:36:01 [error] 25736#25736: *6 rewrite or internal redirection cycle while internally redirecting to "/terrarium/index.html", client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/index.html HTTP/1.1", host: "xxx.com"
2017/05/28 23:41:22 [emerg] 25766#25766: unexpected "}" in /etc/nginx/sites-enabled/default:23
2017/05/28 23:41:43 [emerg] 25775#25775: unexpected "}" in /etc/nginx/sites-enabled/default:23
2017/05/28 23:42:17 [emerg] 25780#25780: unexpected "}" in /etc/nginx/sites-enabled/default:22
2017/05/28 23:47:12 [error] 25850#25850: *7 rewrite or internal redirection cycle while internally redirecting to "/terrarium////////////", client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/ HTTP/1.1", host: "xxx.com"
2017/05/28 23:47:18 [error] 25850#25850: *8 rewrite or internal redirection cycle while internally redirecting to "/terrarium/index.html///////////", client: xxx.xx.xxx.xx, server: 138.68.243.47, request: "GET /terrarium/index.html HTTP/1.1", host: "xxx.com"
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name xxx.xx.xxx.xx; # Replace with your domain

    root /usr/share/nginx/html;
    index index.html;

    client_max_body_size 10G;

    location / {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }

    location /terrarium/ {
        try_files $uri $uri/;
    }
}
/var/www/ghost# cat index.js 
// # Ghost Startup
// Orchestrates the startup of Ghost when run from command line.

var ghost = require('./core'),
    express = require('express'),
    errors = require('./core/server/errors'),
    parentApp = express();

// Make sure dependencies are installed and file system permissions are correct.
require('./core/server/utils/startup-check').check();

ghost().then(function (ghostServer) {
    // Mount our Ghost instance on our desired subdirectory path if it exists.
    parentApp.use(ghostServer.config.paths.subdir, ghostServer.rootApp);

    // Let Ghost handle starting our server instance.
    ghostServer.start(parentApp);
}).catch(function (err) {
    errors.logErrorAndExit(err, err.context, err.help);
});
2 Answers
jtittle1 May 29, 2017
Accepted Answer

@digitalvapor

What are you wanting to serve from other directories?

Generally, with try_files, you'd want to use something such as:

try_files $uri $uri/ /index.html;

You also need to make sure that whatever location block you specify exists in the directory that you are specifying as root.

For example, if a request comes in for /terrarium, then NGINX is, with your current server block, going to look in:

/usr/share/nginx/html/tarrarium

If you need it to look elsewhere for the file(s), you need to add a root directive to the location block to specify that, such as:

location /tarrarium {
    root /my/new/path;

    try_files $uri $uri/ /index.html;
}

You don't, however, want to add tarrarium to root.

For example, this would be incorrect:

location /tarrarium {
    root /my/new/path/tarrarium;

    try_files $uri $uri/ /index.html;
}

The reason for this is because NGINX will append the directory from the location block on to root, so you'd end up requesting a file in:

/my/new/path/tarrarium/tarrarium
  • thank you for good explanation!

    what you said about

    location /terrarium {
        root /my/new/path;
    
        try_files $uri $uri/ /index.html;
    }
    

    was what I needed to fix my issue, thanks!

Have another answer? Share your knowledge.