Backend&Devops/Nginx

[Nginx] connect() failed (111: Connection refused) while connecting to upstream 오류 해결

기은P 2022. 6. 3. 11:29
반응형

1. 개요

Next.js 웹 서버(3000)와 Nginx(80)로 리버스 프록시를 연결하는 과정에서 docker-compose를 사용해 2개의 컨테이너를 묶어서 빌드하는 과정에서 발생했던 오류였습니다.

 

오류내용

*1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.24.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost"

*1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.24.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost"

 

 

혹시나 nginx 설정이 잘못되었나? docker-compose 설정이 잘못되었나? 하고 여러가지 시도를 해보았지만 오류가 풀리지 않았습니다...

proxy_pass 설정도 upstream server로 잘 구성했는데 ㅠㅠ

 

 

2. 해결 방법

아는 개발자 친구한테 물어보니 기본적으로 docker 컨테이너는 격리된 환경으로 localhost라는 아이피로 다른 컨테이너에 접근하지 못한다는 이유였습니다.

 

localhost -> 172.17.0.1로 서버 주소를 변경

위 아이피로 바꾸니 nginx에서 web 서버로 서빙(redirect)이 잘 되는 것을 확인했습니다!

 

아이피 172.17.0.1란?

IP 주소 172.17.0.1은 대부분의 무선 라우터 또는 ADSL 모뎀의 기본 게이트웨이입니다.

라우터는 여러 IP를 로그인 주소로 사용할 수 있지만 172.17.0.1은 공통 주소 중 하나입니다. 기본 IP 액세스 주소는 라우터 브랜드마다 다르며 일반적으로 사용자는 기본 주소를 수정할 수 있습니다. 따라서 특정 접속 주소를 참조하시기 바랍니다. 또한 기본 포트는 일반적으로 80이므로 액세스 주소에서 포트가 생략됩니다(전체 주소는 http://172.17.0.1:80 ). 포트 값이 수정되면 완전히 작성해야 합니다(예 : http://172.17.0.1:8080 ).

라우터를 관리하려면 브라우저의 주소 표시줄에 172.17.0.1 을 입력하세요. 라우터 관리 패널에 성공적으로 액세스한 후 IP Qos, DNS, 프록시, LAN, WAN, 네트워크 관리, 보안 옵션, WLAN 설정, PPPOE, MAC, WPS, DSL 및 DHCP 클라이언트 종료 옵션을 조정하고 설정할 수 있습니다.

출처: https://ko.ipshu.com/ipv4/172.17.0.1

 

 

 

3. 사용된 nginx.conf 및 docker-compose.yml

 

nginx/nginx.conf

user nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

	# 백엔드 upstream 설정
    # upstream myweb-api {
    #     server api:8080;
    # }

	# 프론트엔드 upstream 설정
    upstream next-server {
        server 172.17.0.1:3000;
    }

    server {
        listen 80;

		# /api 경로로 오는 요청을 백엔드 upstream 의 /api 경로로 포워딩
        # location /api {
        #     proxy_pass         http://myweb-api/api;
        # }

		# / 경로로 오는 요청을 프론트엔드 upstream 의 / 경로로 포워딩
        location / {
            proxy_pass         http://next-server/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    # include /etc/nginx/conf.d/*.conf;
}

 

docker-compose.yml

version: "3.8"

networks:
  corp:
    driver: bridge

services:
  nginx_proxy:
    image: nginx:1.21.5-alpine
    container_name: nginx_proxy    
    ports:
      - 80:80
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf

  next-client:
    container_name: next-client
    build:
      context: ./
      dockerfile: ./Dockerfile
    ports:
      - 3000:3000
반응형