MaiDeveloper

How to install or move Ghost blog to a subdirectory using Nginx?

Ghost is a free and open source blogging platform written in JavaScript and distributed under the MIT License, designed to simplify the process of online publishing for individual bloggers as well as online publications.

Configure Ghost

There are two ways to change the config url.

  1. Change it using ghost-cli. Go to your ghost installation directory and execute the following command to change the url of your ghost instance.

    ghost config url https://maideveloper.com/blog
    ghost restart
    

    Be sure to replace https://maideveloper.com/blog to match yours.

  2. Modify the config.production.json file directly.

    {
        "url": "https://maideveloper.com/blog/",
        "server": {
            "port": 2368,
            "host": "0.0.0.0"
        }
    }
    

Configure Nginx

Add the following configuration to your nginx.

    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_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://172.23.0.31:2368;
        proxy_redirect off;
    }

It would look something like this.

server {
    listen       443;
    server_name  maideveloper.com;

    ssl                  on;
    ssl_certificate      fullchain.pem;
    ssl_certificate_key  privkey.pem;

    server_tokens off;
    charset utf-8;
    
    location / {
        proxy_pass http://127.0.0.1;
    }
    
    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_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://172.23.0.31:2368;
        proxy_redirect off;
    }
}



Mike Mai
Mike Mai   Brooklyn, New York
I am full-stack web developer, passionate about building world class web applications. Knowledge in designing, coding, testing, and debugging. I love to solve problems.