Ghost as a sub directory using NGINX


#1

Hi,

I would like my ghost blog to be served from mydomain.com/blog instead of the root.

I followed a tutorial found here: https://www.ghostforbeginners.com/how-to-install-ghost-in-a-subdirectory/
but cannot get it working.

btw, ghost does work when I make it the root, mydomain.com/. So I believe my installation is correct and that I am just having problems using it as a subdirectory.

Here is my block in my NGINX config file.

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://localhost:2368;
proxy_redirect off;
}

Here is my ghost config file

"url":"http://mydomain.com/blog/",
"database": {
    "client": "sqlite3",
    "connection": {
        "filename": "content/data/ghost-dev.db"
    },
    "debug": false
},
"server": {
    "host": "127.0.0.1",
    "port": "2368"
},
"paths": {
  "contentPath": "./content"
}

}
After restarting Ghost, it says "INFO Url configured as: http://mydomain.com/blog/
"
However, I get a 404 Page Not Found page when navigating to /blog. The sample posts are present on the page though without any images or CSS.

Any ideas on possible fixes?
Thanks!


#2

Hi there,

Is there a reason you aren’t using Ghost-CLI? It would sort this configuration out for you automatically.

The tutorial you followed doesn’t have the trailing slash for the location match.

In the templates used by Ghost-CLI (which you can find in the repo via the cli docs the location is specified exactly rather than using the caret-tilde match:

location /blog/ {
   // config here
}

Not 100% sure if this is the cause though - it could be some other part of the file is causing a conflict.


#3

Just made the change, but still no luck.

If I use the Ghost-CLI can I still use a sqlite database? Also, I already have a NGINX config file for the rest of my site. So will the ghost populated NGINX files overwrite, or cause any issues with the existing config file?


#4

Yes of course, the options for ghost install are documented here:

The CLI will create entirely new, separate files, as per the templates I linked to above & then will symlink those into place in /etc/nginx/sites-enabled/. No existing files will be touched.

If there is some configuration elsewhere in your nginx config that “trumps” or conflicts with the Ghost config, it could definitely still cause trouble. Undoing that would be a case of removing the added symlinks and restarting nginx. Our templates deliberately do not use the default_server directive, which reduces most opportunity for conflict.


404 after installation
#5

Hi there,
@belltyler How did it go? Were you successful to run your blog via subdirectory?

I’ll be installing Ghost in my blog folder tonight, mydomain.com/blog, using CLI. Let’s see how it goes. I’ll be calling you @Hannah if ran into trouble. My stack is slightly different but supported. I am using Ubuntu 18.04, SQLite3, and Node8 (LTS).


#6

Yes! Using the CLI resolved the issue. I ended up taking the nginx config file it created, and copying it over to my main nginx config file. Worked perfectly.


#9

Hi there,

I have successfully installed ghost in my subdirectory https://sub.domain.com/articles/ However ghost admin panel at https://sub.domain.com/ghost/ is gone 404. Could you please guide me what’s going wrong with admin panel? How can I enable it and start writing?

Thank you.


#10

If you’ve installed Ghost in a subdirectory https://sub.domain.com/articles/ then your admin panel would live at https://sub.domain.com/articles/ghost/


#11

I am really sorry for the embarrassment Kevin, I am really sorry.


#12

Don’t be sorry, sometimes no matter how long you look at things it just doesn’t click, we’ve all been there :smile:


#13

So true, Kevin. :smile:

It was really a joy to install Ghost on a subdomain then in a subdirectory. I feel stronger than before now. All the great help came from Ghost documentation.