Score:0

Using Reverse Proxy Nginx in a docker container

cn flag

So trying to setting Nginx as a reverse proxy using docker - I have three node js containers (one frontend and two backend services) the frontend service calls the two backend services. So I want nginx to redirect to the frontend (which is web application), but everytime I try to send something I get a 502 bad gateway error. When I directly exec into the docker containers I can ping the other containers via dns_hostname or ip address (which I grab from docker inspect) Here is an edited version of both my docker-compose and my nginx.conf

    version: "3.8"
services:
  frontend:
    image: frontend
    hostname: frontend
    env_file:
      /.env
    networks:
      - network
    restart: on-failure
    expose:
      - "3000"
    container_name: frontend
    backend-service-01:
      image: backend-service-01
        hostname: backend-service-01
        env_file:
            ./backend-service-01/.env
        networks:
            - network
        restart: on-failure
        container_name: backend-service-01
   backend-service-02:
        image: backend-service-02
        hostname: backend-service-02
        env_file:
            ./backend-service-02/.env
        networks:
            - network
        restart: on-failure
        container_name: backend-service-02
        
   nginx-proxy:
     image: nginx:stable-alpine
     hostname: nginx-proxy
     networks:
       - network
     restart: on-failure
        volumes:
          - /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
          - /data/nginx/certs/crt_file.crt:/etc/nginx/crt_file.crt
          - /data/nginx/certs/priv_key.key:/etc/nginx/priv_key.key:ro
        ports:
            - 80:80
            - 443:443
        container_name: nginx-proxy
        depends_on: 
          - frontend
          - backend-service-01
          - backend-service-02
networks:
    network:

As well here is the nginx conf file I'm using:

worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 1024;
}

http {

  upstream frontend {
    server frontend:3000;
  }
    
  server {
        
        # Listen Directive on Port 443 (TLS)
        listen              443 ssl; 
        listen              [::]:443 ssl;

        ssl_certificate     /etc/nginx/crt_file.crt;
        ssl_certificate_key /etc/nginx/priv_key.key;

        # Where we store our error.log and access.log
        error_log           /var/log/nginx/error.log;
        access_log          /var/log/nginx/access.log;

        location / {
            root /usr/src/app/public/;
            proxy_pass https://frontend;
        }
  }
}

I've almost done everything to rule out a networking issue, as I can ping both the ip address and the frontend docker hostname from the inside of the nginx container (using docker exec -it nginx-proxy sh) is anyone has any suggestions on what to try next or they can see clearly if I'm doing something wrong it would be much appreciated.

djdomi avatar
za flag
1) EITHER `proxy_pass` 2) OR `root /path` 3) i would suggest using the IP, I am also missing `server_name` and also `proxy_set_header Host $http_host;`
Score:0
cn flag

Issue solved - The issue was neither Docker nor Nginx. The issue was a former developer trying to be helpful that ended up kicking our ass in the long run. They created a redirect in their code that if someone tried to use the HTTP protocol it would automatically redirect to HTTPS protocol, once we commented this pesky couple lines of code out and rebuilt the container, surprise, surprise... Nginx did as it was told. Lesson learned: Get Nginx to do your redirecting; not your code.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.