502 Bad Gateway using nginx port 80, but site loads fine at IP:2368 or domain:2368

  • Posted on May 17, 2014
  • brendaAsked by brenda

I have everything set up with my sudoer account on my Ubuntu VPS, e.g. /ghost directory and sub content owned by sudoer account. I’ve added my domain to the ocean domain DNS and can ping my domain and get back my server’s IP, so this is working fine. I had configured Ghost first, so I am able to get to my site via the IP:2368 and domain:2368 fine, but I wanted to use the nginx proxy server, so I could access it via port 80. Therefore, I setup nginx, I started the service, and when I got to my site (e.g., I get “502 Bad Gateway”. When looking at the errors logs, I see the following:

2014/05/17 16:40:40 [error] 29645#0: *21 connect() failed (111: Connection refused) while connecting to upstream, client: XX.XXX.XXX.XXX, server: request: “GET / HTTP/1.1”, upstream: “”, host: “

I can see nginx is running:

sudo service nginx status

  • nginx is running

The nginx config file contains the usual:

server { listen; server_name my-domain-name; access_log /var/log/nginx/my-domain-name.log;

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

    proxy_redirect off;


My sudoer account is a member of www-data, so my /ghost folder is owned by the sudoer and the www-data group. Both have read, write and execute permissions to all the folders and files in /ghost only. Do I need to change permissions anywhere else for this to work? I’ve been reading other people’s problems with error 502 and they all seem to solve them by changing permissions on their /ghost directory, but I think they are using root and that is not my case.

Note: I get the same error if I log in a root, restart nginx, and load my page, or if I log in as sudoer, restart nginx, and load my page.

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.

Hello Everyone, <br> <br>I wanted to share that I found the issue. When setting up my blog, I initially set it up, so that I could run Ghost with npm and access the blog via my server’s IP address and domain name, so I had changed this in the config.js file. However, I did not want to have to use the the Ghost port 2368, so I installed the nginx proxy server, so that i could re-direct requests on port 80 to 2368. Once nginx was configured, I was getting the Gateway error because both ghost and nginx where now listening at different ports and nginx was redirecting to the server’s localhost address at port 2368. The problem here was the Ghost’s config.js was still using the server’s IP, rather than the localhost address. Anyways, once I change the Ghost config.js file’s “host” address back to the localhost, everything worked. In case anyone has this problem and want’s to see how I got it working, you can check my post at <br> <br>

Can you paste the output of “nginx -t” please?