Miguel Herreros Cejas
Miguel Herreros Cejas

Reputation: 674

Configure Nginx with two docker-compose containers (Flask and Django)

I am trying to expose two different address used like APIs. One in Django and the other one in Flask, they are Docker-compose containers.

I need configure Nginx for expose the two containers in two different subdomains.

It is my Nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
 worker_connections  1024;  ## Default: 1024, increase if you have lots of clients
 }    

http {
  include       /etc/nginx/mime.types;
  # fallback in case we can't determine a type
  default_type  application/octet-stream;

 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;
#tcp_nopush     on;

keepalive_timeout  65;

upstream app {
 server django:5000;
}

upstream app_server {
 server flask:5090;
}

server {
 listen 5090;
 location / {
    proxy_pass http://app_server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Scheme $scheme;
  }
}

server {
 listen 5000;
 location / {
    proxy_pass http://app;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Scheme $scheme;
 }
 }

}

And my production.yml

Nginx:
  build: ./compose/production/nginx
  image: *image
  ports:
    - 80:80
  depends_on:
    - flask
    - django

My containers are all up.

enter image description here

Upvotes: 1

Views: 489

Answers (1)

Andrey RF
Andrey RF

Reputation: 342

I use proxy_pass:

server {
    listen <port>;
    location / {
        proxy_pass http://<container-host-name>:<port>;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Scheme $scheme;
    }
}

You nginx container connected only with 80 port on machine and 80 port on container, but you nginx server listen 5000 and 5090 ports :)

Upvotes: 1

Related Questions