Question

Nginx does not restart when configuring subdomain for running ghost

Ive been trying to get this working for about 8 hours now. I am new to all this configuration and read a lot of DO articles and nginx articles and every term that I came across was new to me. I got pretty close to getting it working but I am not there yet.

Here’s what I am trying to do mydomain.com : The default port 80 should redirect to port 3000 on which I run my nodejs project. (I was able to do this by setting a server block in nginx)

sub.mydomain.com : This should redirect to port 2368 on which I have setup my ghost blog (This is where I am stuck. I setup the server block in nginx but then the server wont start)

–Here’s what I have done/tried so far–

DNS Setup I created an A record with the subdomain name and pointed it to the droplet IP address(and I have remembered to include the trailing dot at the end of the domain name in the A record).

$TTL 1800 @ IN SOA NS1.DIGITALOCEAN.COM. hostmaster.quodocs.com. ( 1398013797 ; last update: 2014-04-20 17:09:57 UTC 3600 ; refresh 900 ; retry 1209600 ; expire 1800 ; ttl ) IN NS NS1.DIGITALOCEAN.COM. NS NS2.DIGITALOCEAN.COM. NS NS3.DIGITALOCEAN.COM. @ IN A 1.2.3.4

Ghost Setup

  1. Installed Ghost.
  2. Updated the ghost config.js production url to sub.domain.com
  3. Ran ghost in production mode and verified that its working on domain.com:2368 or sub.domain.com:2368
  4. Since I have a wildcard CNAME blah.domain.com:2368 works too but I am ok with that.

My own Nodejs application

  1. ran my nodejs server on port 3000 and verified that its working.

Nginx

  1. Installed nginx
  2. Removed the soft link for “default” in sites-enabled
  3. created a file nginx/sites-available/main with the following contents

server { listen 0.0.0.0:80; server_name mydomain.com; access_log /var/log/nginx/mydomain.com.log;

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://127.0.0.1:3000;
    proxy_redirect off;
}

} 4) Added a softlink to sites-available/main from sites-enabled/main

I restarted nginx, and so far so good. The proxy works as intended and any requests to the mydomain.com or sub.mydomain.com or blahblah.mydomain.com are going to my Node project on port 3000.

Now the trouble begins. I created a file nginx/sites-available/ghost with the following contents server { listen 0.0.0.0:80; server_name sub.mydomain.com; access_log /var/log/nginx/sub.mydomain.com.log;

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://127.0.0.1:2368;
    proxy_redirect off;
}

}

Then, I added a softlink to sites-available/ghost from sites-enabled/ghost

Then i try to restart my nginx server and it wont start. If i delete the soft link to ghost and try to restart, it works.

My questions are What is wrong with the nginx proxy config for the ghost file? Because the moment remove the softlink, nginx is able to start. Have I done the right thing by configuring an A record? If no, what should I use as the CNAME? If yes, will the above A record config work once the nginx proxy config file for the ghost is fixed?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Guys, after a lot more hair pulling, I finally managed to fix it. <br>Seems like a gateway error is when the upstream server (the server being redirected to is giving issues). <br>In my case, the ghost server was running, so I find anything wrong. <br>But then upon closer examination, i realized that in my ghosts config.js file, i had specified the ipaddress of my droplet in the production settings. <br>I changed the ip address to 127.0.0.1 <br>And that was all i needed to do. <br> <br>Another hurdle crossed!

@Kamal <br> <br>I was able to start nginx after doing what you said. <br>I also updated the DNS and removed the wildcard. <br> <br>BUT, I am getting a Bad Gateway error this time. <br> <br>In my browser, <br>Opening sub.domain.com:2368 = ghost blog appears <br>If i just open sub.domain.com = Bad Gateway <br> <br>Something wrong with the proxy config i guess? <br>Here’s what I have <br> <br>server { <br> listen 0.0.0.0:80; <br> server_name sub.domain.com; <br> access_log /var/log/nginx/sub.domain.com.log; <br> <br> location / { <br> proxy_set_header X-Real-IP $remote_addr; <br> proxy_set_header HOST $http_host; <br> proxy_set_header X-NginX-Proxy true; <br> <br> proxy_pass http://127.0.0.1:2368; <br> proxy_redirect off; <br> } <br>} <br> <br> <br>Thanks for the help! <br> <br>

The A record is fine, but I would delete the wildcard CNAME record and add non-wildcard records as needed. <br> <br>Run <code>nginx -t</code> after adding the symlink back – if it complains about not being able to build the server names hash, edit <code>/etc/nginx/nginx.conf</code> and set <code>server_names_hash_bucket_size</code> to <code>64</code>. <br> <br>Let me know how it goes!