Nginx gives 502 Bad Gateway when proxying to nodejs app running on different docker container

I am using docker compose to create 2 containers one for application running on Nginx and another for backend application on Nodejs(running on another port 4000) with routes of patterns “/org-metadata/, /proxy-api/, /node-api/**”.

I am trying to proxy the nginx to nodejs running on 4000, but it does not reach the backend. and the nginx gives the 502 Bad Gateway response.

P.S: my node app gives 503 service unavailable when I run it from browser.

Can some one pls point where am I missing anything?

Here is my nginx.conf file:

worker_processes auto;

events {
    worker_connections 8000;
    multi_accept on;

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log /dev/stdout;
    error_log /dev/stderr;

    upstream backend {

    server {
        listen 80;

        root /var/www;
        index index.html index.htm;

        location / {
            try_files $uri $uri/ /index.html;

        location ~ (proxy-api|node-api|\/api\/) {
            proxy_pass http://backend;
            proxy_redirect off;
            add_header X-Frame-Options "SAMEORIGIN" always;
            add_header X-XSS-Protection "1; mode=block" always;
            add_header X-Content-Type-Options "nosniff" always;
            add_header Referrer-Policy "no-referrer-when-downgrade" always;
            add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;


version: "3"

            context: "./server"
            - "4000:4000"
        image: nodejs
        container_name: nodejs
        restart: unless-stopped
        build: "."
            - "80:80"
        image: webserver
        container_name: webserver
        restart: unless-stopped
            - nodejs

Dockerfile for nodejs:

FROM node:11-alpine
COPY . .
RUN npm ci
CMD [ "npm", "run", "start" ]

Dockerfile for nginx:

FROM nginx:1.15.2-alpine
COPY ./build /var/www
COPY nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT ["nginx","-g","daemon off;"]

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.

Bobby Iliev
Site Moderator
Site Moderator badge
August 22, 2019
Accepted Answer


I’ve tried testing your setup, what fixed the problem at my end was adjusting this part in the Nginx config:

    upstream backend {


    upstream backend {
        server your.droplet.ip.address:4000;

You could also change the Droplet IP with the name of your docker container.

Then Nginx would be able to communicate with the node container on port 4000.

Another thing that you might want to add, as ff you are using an upstream block, it may be advisable to set the Host header explicitly:

proxy_set_header Host;

Hope that this helps! Regards, Bobby

Thanks @bobbyiliev it worked. Another issue, the API I called continuously redirects with 302, is it something to do with my API or nginx config?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel