ryanas101
By:
ryanas101

Nginx does not restart when configuring subdomain for running ghost

April 20, 2014 1.4k views
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 * CNAME @ sub.mydomain.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?
3 Answers
The A record is fine, but I would delete the wildcard CNAME record and add non-wildcard records as needed.

Run nginx -t after adding the symlink back -- if it complains about not being able to build the server names hash, edit /etc/nginx/nginx.conf and set server_names_hash_bucket_size to 64.

Let me know how it goes!
@Kamal

I was able to start nginx after doing what you said.
I also updated the DNS and removed the wildcard.

BUT, I am getting a Bad Gateway error this time.

In my browser,
Opening sub.domain.com:2368 = ghost blog appears
If i just open sub.domain.com = Bad Gateway

Something wrong with the proxy config i guess?
Here's what I have

server {
listen 0.0.0.0:80;
server_name sub.domain.com;
access_log /var/log/nginx/sub.domain.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;
}
}


Thanks for the help!

Guys, after a lot more hair pulling, I finally managed to fix it.
Seems like a gateway error is when the upstream server (the server being redirected to is giving issues).
In my case, the ghost server was running, so I find anything wrong.
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.
I changed the ip address to 127.0.0.1
And that was all i needed to do.

Another hurdle crossed!
Have another answer? Share your knowledge.