0

I am struggling with the connection of nginx and django (docker container)

My strategy is like this , run uwsgi http option and 8001 port. (not socket)

uwsgi --http :8001 --module myapp.wsgi --py-autoreload 1 --logto /tmp/mylog.log

then I confirmed wget http://127.0.0.1:8001 works.

but from, nginx, It can't connect somehow. (111: Connection refused) error

However from nginx wget http://django:8001 works.

How can I connect between containars

upstream django {
    ip_hash;
    server 127.0.0.1:8001;
}

server {
    listen      8000;
    server_name 127.0.0.1;
    charset     utf-8;

    location /static {
        alias /static;
    }

    location / {
        proxy_pass  http://127.0.0.1:8001/;
        include     /etc/nginx/uwsgi_params;
    }
}

server_tokens off;

I am trying this config but, if I try this, my container doesn't launch.

log is like this 2020/03/24 08:24:04 [emerg] 1#1: upstream "django" may not have port 8001 in /etc/nginx/conf.d/app_nginx.conf:16

upstream django {
    ip_hash;
    server django:8001;
}

server {
    listen      8000;
    server_name 127.0.0.1;
    charset     utf-8;

    location /static {
        alias /static;
    }

    location / {
        proxy_pass  http://django:8001/;
        include     /etc/nginx/uwsgi_params;
    }
}

server_tokens off;

my docker compose is very simple...

  nginx:
    image: nginx:1.13
    container_name: nginx
    ports:
      - "8000:8000"
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
      - ./nginx/static:/static
    depends_on:
      - django

Finally thanks to helps. my server works. final conf is like this below.

remove upstream and use name 'django' instead of 127.0.0.1

server {
    listen      8000;
    server_name 127.0.0.1;
    charset     utf-8;

    location /static {
        alias /static;
    }

    location / {
        proxy_pass  http://django:8001/;
        include     /etc/nginx/uwsgi_params;
    }
}

server_tokens off;
whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

2

If they run in different containers, 127.0.0.1 is not the correct IP; use the name of the other container, e.g.

proxy_pass http://django:8001;

so Docker's internal DNS can route things.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thank you very much your answer might be hit the spot . But when I use `proxy_pass http://django:8001;` my container doesn't launch. – whitebear Mar 24 '20 at 08:17
  • By the way, you don't need that `upstream` block with a simple `proxy_pass`. – AKX Mar 24 '20 at 08:20