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

August 21, 2019 255 views
Nginx Docker Node.js React

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 127.0.0.1:4000;
    }

    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;
        }
    }
}


docker-compose.yml

version: "3"

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

Dockerfile for nodejs:

FROM node:11-alpine
WORKDIR /app
COPY . .
RUN npm ci
EXPOSE 4000
CMD [ "npm", "run", "start" ]

Dockerfile for nginx:

FROM nginx:1.15.2-alpine
COPY ./build /var/www
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
ENTRYPOINT ["nginx","-g","daemon off;"]
2 Answers
bobbyiliev MOD August 22, 2019
Accepted Answer

Hello,

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

    upstream backend {
        server 127.0.0.1:4000;
    }

To:

    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 example.com;

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?

  • Hi @rgth

    Yes, I noticed the same thing at my end, what fix the issue for me was adding this proxy header in the Nginx config:

    proxy_set_header Host example.com;
    

    Let me know how it goes!

    Regards,
    Bobby

    • dint work :(

      actually my nodejs API which is http proxies requests to another services and those services only https requests. how do I make my nodejs API behave like https?

      I tried using https.createService in my node API but dint work.

      • Hello,

        You could try using this npm module here:

        https://www.npmjs.com/package/https-proxy-agent

        As far as I can see it provides an http.Agent implementation that connects to a specified HTTP or HTTPS proxy server, and can be used with the built-in https module.

        Hope that this helps!
        Regards,
        Bobby

        • tried these earlier, dint work..

          Also I observed that browser is not sending the cookies to the proxied requests in the request headers.

          Where as when I run the app loaclly it sends the header with key “Cookie” with all cookies as its value.

          Is there anything I need to add in nginx.conf for it to send cookies like this in header?

          • Hello,

            What you could try also is adding this extra proxy header:

                    proxy_set_header   X-Forwarded-For $remote_addr;
            

            Let me know how it goes!

Have another answer? Share your knowledge.