DEV Community

Nginx as reverse proxy for a flask app using Docker

Ishan Khare on March 08, 2019

So what is a reverse proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more se...
Collapse
 
codesytex profile image
CodeSytex

Hello,

I follow your example and when I start the docker-compose at the end of your guide (with the configuration of the proxy pass) I have the error :

2020/03/04 15:33:50 [emerg] 1#1: host not found in upstream "flask-app" in /etc/nginx/conf.d/default.conf:6

nginx: [emerg] host not found in upstream "flask-app" in /etc/nginx/conf.d/default.conf:6

I checked and all my files are like your example.

Any ideas ??

Thanks !

Collapse
 
ishankhare07 profile image
Ishan Khare

As I mention in point 4

The proxy_pass directive takes as argument the url to which we are proxying. In our case we will alias our flask container in the docker network as flask-app. This makes our flask app accessible at flask-app:5000/ from inside the nginx container.

Collapse
 
ishankhare07 profile image
Ishan Khare • Edited

Also did you forget adding alias?

networks:
    my-network:
        aliases:
            - flask-app
Collapse
 
ishankhare07 profile image
Ishan Khare

Can I look at your sample code, is available in some repo?

Collapse
 
gordaansiosa profile image
gordaansiosa

Hello,
I am new to docker, I have a bitwarden instance running using a nginx reverse-proxy. nginx stops working after a while, but I don't know why, I have checked the logs (docker logs container_id) without finding any issue.
I know I am not providing that much information, but I don't know where to start, honestly.

Collapse
 
ishankhare07 profile image
Ishan Khare

Is the nginx running as a docker container?
If so you might want to look into your nginx access and error logs

Collapse
 
gauravbhandari19 profile image
Gaurav Bhandari • Edited

It was a very well written post, thank you for that :)
However, I think the docker-compose.yml should have, this?
volumes:

  • ./code/:/code/

Instead of :

volumes:

  • ./:/code/
Collapse
 
ishankhare07 profile image
Ishan Khare

It depends on what your $PWD is

Collapse
 
gauravbhandari19 profile image
Gaurav Bhandari

Correct, but perhaps you can add directory structure too, else it gets confusing.

Collapse
 
paupaulaz profile image
paupaulaz

Very nice tutorial, thanks !

From a security / logic standpoint, I would just recommend removing the "ports" section in the flask service of your docker-compose.yml file. Indeed, since both of your containers are on the same docker network container, they can "talk" to each other without exposing ports.

In your current situation, I think Flask would be reachable directly by hitting :5000, which could remove some of security layers that motivated the use of nginx.

Hope that helps, thanks again for the great post :)

Collapse
 
atul1234anand profile image
atul1234anand

Is my-network left empty?

Collapse
 
ishankhare07 profile image
Ishan Khare

Yes in this case it is. It will create a network namespace which both containers will be part of. If you require additional configuration of the network then my-network can be extended accordingly.