简体   繁体   English

Nginx 和 Gunicorn 与 Docker - 如何限制对 Flask 应用程序的访问仅限 ZEE434023CF89D7DFDB74FZ3D640D7DFDB74F6?

[英]Nginx and Gunicorn with Docker - How to limit access to Flask app to only nginx?

My current Flask and Docker setup is the following:我当前的 Flask 和 Docker 设置如下:

带有 Docker 设置的当前 Flask

In this scenario, end users access the application container from the external IP on port 80 (which is a request to the Nginx container), which then proxies the request over to application container running on port 8000 with Gunicorn.在这种情况下,最终用户从端口 80 上的外部 IP 访问应用程序容器(这是对 Nginx 容器的请求),然后将请求代理到使用 Gunicorn 在端口 8000 上运行的应用程序容器。 However, people can bypass Nginx by using the same URL, but specifying the port as 8000, accessing the app directly using Gunicorn.但是,人们可以使用相同的URL绕过Nginx,但将端口指定为8000,直接使用Gunicorn访问应用程序。

How do I limit access to my Flask application so it is only accessible via the Nginx container?如何限制对我的 Flask 应用程序的访问,使其只能通过 Nginx 容器访问? If I block port 8000 in my server's firewall, would Nginx still be able to proxy to the container running on port 8000?如果我在服务器的防火墙中阻止端口 8000,Nginx 是否仍然能够代理到在端口 8000 上运行的容器?

If you --bind=127.0.0.1:800 ( doc ) then gunicorn should be limited to the (docker) host.如果你--bind=127.0.0.1:800 ( doc ) 那么gunicorn应该被限制在 (docker) 主机上。

You should test this to confirm.您应该对此进行测试以确认。

Use communication across links.使用跨链接通信。 Links allow containers to discover each other and securely transfer information about one container to another container.链接允许容器相互发现并将有关一个容器的信息安全地传输到另一个容器。 When you set up a link, you create a conduit between a source container and a recipient container.设置链接时,您会在源容器和接收容器之间创建管道。 The recipient can then access select data about the source.然后收件人可以访问有关源的 select 数据。 To create a link, you use the --link flag.要创建链接,请使用 --link 标志。

First start you app.首先启动你的应用程序。

docker run -d --name my_app APP_IMAGE

Then start nginx container with link to your app container.然后启动 nginx 容器并链接到您的应用程序容器。

docker run -d -P --name nginx --bind=YOUR_NGINX_BIND --link my_app:my_app NGINX_IMAGE

Your application is now accessible from your nginx container via the alias "my_app" (no ip address needed).您的应用程序现在可以通过别名“my_app”从您的 nginx 容器访问(不需要 ip 地址)。 It will not be available from the host.它将无法从主机获得。

More info: https://docs.docker.com/network/links/#communication-across-links更多信息: https://docs.docker.com/network/links/#communication-across-links

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM