4

I have two docker container running, one is a nginx as reverse-proxy on 80,443 that accepts http and https requests and passes them to the another container which is a Python Flask App container running on port 8000.

If I run nginx on host machine instead of running it in a container, flask app can read the correct client ip address but

if I run nginx also in a container, it shows only docker0 network interface's ip address to the flask application running in 2nd container, no matter what the actual client ip is.

In Python Flask app I use the following code:

ip = request.environ.get('HTTP_X_REAL_ip', request.remote_addr)

Docker-Compose File:

mongo:
  restart: always
  image: mongo:latest
  ports:
    - '27017'
  volumes:
    - /opt/python/apps/myapp/PersistentDockerVolumes/mongo/data:/data/db/

redis:
  restart: always
  build: ./redis
  command: ["redis-server", "/usr/local/etc/redis/redis.rb"]
  ports:
    - '6379'

myapp:
  restart: always
  build: ./myapp
  command: ["gunicorn", "--config=gunicorn.py", "myapp:app"]
  environment:
    DEBIAN_FRONTEND: 'noninteractive'
    PYTHONUNBUFFERED: 'true'
  links:
    - mongo
    - redis
  volumes:
    - /opt/python/apps/myapp/logs/myapp/:/myapp/log/
  ports:
    - '8000'

nginx:
  restart: always
  build: ./nginx
  command: ["nginx", "-g", "daemon off;"]
  links:
    - myapp
  volumes:
    - /opt/python/apps/myapp/logs/nginx:/etc/nginx/logs
  volumes_from:
    - myapp
  ports:
    - '80:80'
    - '443:443'

In the nginx config, I have defined an proxy pass to myapp:

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

I tried looking for solution, but none of them is for python flask application. My scenario is similar to this, but the difference is I have it for Flask instead of Jetty and the solution is very specific for Jetty.

Another thread which is partially similar is here, scenario discussed in this thread is nginx server behind nginx reverse proxy, which is again not the same scenario, and I am skeptical about running docker with iptables disabled.

Community
  • 1
  • 1
  • I have a reverse proxy nginx container (not using the jwilder one) that uses an upstream config to a python tornado server. Do you have one of those set? It just uses the hostname of the other container – OneCricketeer May 07 '16 at 00:26
  • 1
    It *looks* like this is a duplicate - if `request.access_route` doesn't have the *real* IP, just ping me with a comment here and I'll re-open. – Sean Vieira May 07 '16 at 02:20
  • Thanks @Sean for pointing this out. I tried looking for possible duplicates but I couldn't find the exact one you have shared. I looked for all possible similar questions while writing my question but none of it was discussed in same context compare to what I was facing. I guess I wouldn't have found it myself as I didn't knew it was a flask problem, after reading the github link that I have mentioned in my question, I was thinking that it is a docker issue. – Ishan Girdhar May 07 '16 at 10:40

0 Answers0