Question

Nginx gives 502 Bad Gateway when i try to acess my node js aplication running on different docker container

So I’m trying to use NGINX as a reverse proxy for 2 react apps and 1 node js api. Each in separate docker containers.

The restapi service must be hosted on a seperated port as the portal by default. The nginx proxy configuration must include a route for the Portal on port 443 and a route for the restapi and admin portal must have a seperate route entry on a different Port like 8443.

With my current nginx configuration I expect the following behavior: localhost leads to one react app called portal, localhost/adminportal/ -> leads to another react app admin portal, localhost/api-portal/ -> leads to the api fo the portal app, and localhost/api-adminportal/ -> leads to the api fo the admin portal app

But at this moment only the first and second example (localhost/ and localhost/adminportal/) is working properly, the other two show a nginx error page.

And When i try to acess localhost/api-portal/ i have this error in my log file of my nginx container:

2022/04/10 20:43:33 [error] 30#30: *57 upstream prematurely closed connection while reading response header from upstream, client: 172.28.0.1, server: localhost, request: "GET /api/ HTTP/1.1", upstream: "http://172.28.0.3:3333/", host: "localhost:8443"

172.28.0.1 - - [10/Apr/2022:20:43:33 +0000] "GET /api/ HTTP/1.1" 502 497 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36" "-"

This is my nginx configuration:

server {
    listen 80;
    server_name localhost 127.0.0.1 0.0.0.0;
    return 301 https://$server_name$request_uri;
}
    
server {
    listen 443 ssl;
    server_name localhost 127.0.0.1 0.0.0.0;

    ssl_certificate /etc/nginx/cert/cert.pem;
    ssl_certificate_key /etc/nginx/cert/key.pem;

    location / {
        proxy_pass http://fpr-frontend:3000/;
    }


    # include /etc/nginx/conf.d/koma/*.conf;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }port
}

server {
    listen 8443 ssl;
    server_name localhost 127.0.0.1 0.0.0.0;

    ssl_certificate /etc/nginx/cert/cert.pem;
    ssl_certificate_key /etc/nginx/cert/key.pem;

     location /adminportal/ {
        proxy_pass http://fpr-adminportal:3001/adminportal/;
    }

    location /api-adminportal/ {
        proxy_pass http://fpr-backend:3335/;
    }

    location /api-portal/ {
        proxy_pass http://fpr-backend:3333/;
    }

    # include /etc/nginx/conf.d/koma/*.conf;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

Below is my docker-compose file:

version: "3.2"

volumes:
  mongodata:

services:
  fpr-backend:
    build:
      context: .
      dockerfile: Dockerfile.backend
    image: fpr-backend
    env_file: ./config/fpr-backend.env
    environment:
      FRONTEND_URL: http://localhost:8000/
      FRONTEND_ADMIN_PORTAL_URL: https://localhost:8443/adminportal/
    
    depends_on:
      - mongo
    expose:
      - "3333"
      - "3335"
    ports:
      - "3333:3333/tcp"
      - "3335:3335/tcp"
    command: ["yarn", "start"]

  fpr-frontend:
    build:
      context: .
      dockerfile: Dockerfile.frontend
      args:
       VITE_BACKEND_URL: https://localhost:3333/
    image: fpr-frontend
    env_file: ./config/fpr-frontend.env
    depends_on:
      - fpr-backend
    expose:
      - "3000"
    command: ["yarn", "serve", "--host=fpr-frontend", "--port=3000"]

  fpr-adminportal:

    stdin_open: true # docker run -i
    tty: true        # docker run -t
    build:
      context: .
      dockerfile: Dockerfile.adminportal
      args:
        PORT: 3001
        REACT_APP_API_URL:  https://localhost:3335/
  
    image: fpr-adminportal
    env_file: ./config/fpr-adminportal.env
    environment: 
      - PUBLIC_URL=https://localhost:8443/adminportal/
    depends_on:
      - fpr-backend
    expose:
      - "3001"
    command: ["yarn","dev"]  

  mongo:
    image: mongo
    env_file: ./config/mongo.env
    volumes:
      - mongodata:/etc/mongo


  nginx:
    image: nginx
    depends_on:
      - fpr-backend
      - fpr-frontend
      - fpr-adminportal
    volumes:
      - ./config/nginx/conf.d/:/etc/nginx/conf.d/
      - ./modules/fpr-backend/certificates/UserPortal:/etc/nginx/cert
    expose:
      - "443"
      - "8443"
    ports:
      - "8000:80/tcp"
      - "443:443/tcp"
      - "8443:8443/tcp"

fpr-backend(api) dockerfile:

FROM node:14-alpine
WORKDIR /source
COPY ./modules/fpr-backend/package.json .
COPY ./modules/fpr-backend/yarn.lock .
RUN yarn
COPY ./modules/fpr-backend/ .
EXPOSE 3333
EXPOSE 3335
CMD [ "yarn" , "start" ]


Submit an answer
Answer a question...

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
December 26, 2022

Hi there,

Do you also see any errors in the fpr-backend container logs? The Nginx configuration itself looks correct, the error that you are getting indicates that the fpr-backend container is not handling the requests correctly or that the port it is listening on is not correct.

If you try to make the request directly to the container itself without going over the Nginx proxy, is it able to handle the request as expected?

Best,

Bobby