[SOLVED] Need help in configuring Ghost on subdomain

Hi,

  
 
I installed Ghost manually on an Ubuntu 16.04 VPS by following this tutorial on Vultr docs.

I wanted to install Ghost on my subdomain so I added an A record pointing towards my VPS in DNS settings of my VPS.
 

A      blog      X.X.X.X

 
While Ghost installation, when It asked me for Enter your blog URL, I entered
 
https://blog.example.com

Now my blog is up and running at blog.example.com but when I open my VPS IP directly or visit example.com, It shows me the default Welcome to nginx!.

 
 
I actually want It to get redirected to my subdomain (where my blog is).

example.com => blog.example.com
X.X.X.X          => blog.example.com

 
 
Any idea how can I do this?
While searching, I discovered that It can be done via NGINX configuration settings.

 
Somewhere It was mentioned that I need to edit the following file and include a code block at the end of It.
 
/etc/nginx/conf.d/default.conf

but I’m unable to find default.conf in the above-mentioned location.

 

Though I have discovered the default.conf in the following directory

/etc/nginx/sites-enabled
 
but on pasting the code, It actually stops loading my main site (example.com).

You need to use Ghost-cli to setup Nginx for you. I had a similar problem caused by misconfigurations.

Go to the Ghost directory. Run command

ghost setup

Enter the information missing or update. Continue …
When it asks to use Nginx say yes.

Rest it will handle.

Restart the Ghost to reflect new changes.

I’ve already installed Ghost using Ghost CLI with ‘Yes’ for Nginx.

You need something like this in your nginx site config:

server_name example.com;
return 301 $scheme://blog.example.com$request_uri;

Thank you for this code.

I just added your code block (after tweaking a little bit) to /etc/nginx/default.conf

server {
	server_name example.com www.example.com;
	return 301 https://blog.example.com$request_uri;
}

Now the www.example.com is redirecting successfully to https://blog.example.com
but when I tried https://example.com It said that This site can’t be reached
 
 

I know that is happening because in my default.conf, It’s only listening for :80

And I think that listening for :443 won’t work because the https is configured for my subdomain only https://blog.example.com.

 
Do I have to add one more https certificate for my main domain https://example.com?

*I’m using LetsEcnrypt (It asked me during installation via ghost-cli).

I have this in my config:

listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;

And yes, you neeed a certificate for main domain as well.

I also have this in my nginx.conf:

server  {
        listen 80;
        listen [::]:80;
        listen 443 default_server ssl http2;
        listen [::]:443 default_server ssl http2;
        ssl_certificate         /srv/www/certs/domain.com.pem;
        ssl_certificate_key     /srv/www/certs/domain.com.key;
        server_name localhost;
        return 444;
        }
}

Thank you for sharing this.
I’ll be marking this as solved. I almost got the results what I wanted (just 1 redirection is causing problem and I was going to figure It out unless I was hit with LetsEncrypt rate limit).

Anyways, If anyone wants to get the same results as me, I’ll suggest installing NGINX first then setting up a LetsEncrypt certificate on main domain.
Afterwards install Ghost on a subdomain.
Then redirect from main domain to subdomain.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.