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.