1

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

3
  • 1
    Well you are telling nginx to run on port 80 then you have server blocks listening on port 5000 with no server_name. So if this is your actual config then yes you would be having some trouble. Commented Apr 19, 2020 at 21:38
  • Hi, thanks for your response. So, Where I must to change the ports? Because If I change ports in production.yml and replace 80:80 by 5090:5090 I have a error. Commented Apr 20, 2020 at 7:36
  • ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint core_nginx_1 (7b49e70c117f45cf00fde7488626897a22657cdf73ac9d07d1d546b55986ae7d): Bind for 0.0.0.0:5090 failed: port is already allocated Commented Apr 20, 2020 at 7:44

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

5 Comments

I changed the configuration, but not in production.yml because If I replace ports, it response my a error. Now, I edited the post with the configuration like your answer, and I have containers working.
You have an error when you change machine port because http use 80 port by default. You can change your container port like 80:5000 or 80:5090. But in this case you will have only one server. I would suggest you to make one server that will be list 5000 port, in production.yml change port to 80:5000, and use different location like /django and /flask. With this configuration when you will have the request like http://<ip>/django it will be proxy to django machine and when http://<ip>/flask to flask machine. But you should to set port for proxy_pass too
Finally, I deleted Flask container. The only reason to use it is because pandas not working in python:alpine. I won't use alpine and I will use python.
I hope you understand what you need to do if you have two and more containers :)
Now, I have an other problem, but It is other history: stackoverflow.com/questions/61330761/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.