可从互联网访问的 gunicorn 反向代理

时间:2021-07-30 16:19:26

标签: nginx gunicorn

我配置了 nginx 和 gunicorn 来为 Flask 应用程序提供服务。我用这个命令启动了 gunicorn gunicorn --bind 0.0.0.0:5000 wsgi:app 我的网站可以从我提供的 80 端口上的 IP 地址访问。但是它也可以在端口 5000 上访问。看起来我的反向代理可以正常工作,但也可以访问 gunicorn 服务器。

我打算禁用端口 5000,但不确定这是解决此类问题的正确、安全的方法。

这是我的 nginx 配置文件:

server {

  server_name <my_ip_adress>;
  access_log /var/log/nginx/domain-access.log;

  location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For  $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;

    # This line is important as it tells nginx to channel all requests to port 5000.
    # We will later run our wsgi application on this port using gunicorn.
    proxy_pass http://127.0.0.1:5000/;
  }

}

1 个答案:

答案 0 :(得分:1)

您正在将 gunicorn 绑定到 --inspect,因此它可以在外部接口上使用。假设这只是一个盒子:

0.0.0.0

这不再侦听来自外部接口的请求,这意味着所有请求都必须通过 nginx。

当然,如果您确实将 gunicorn 绑定到 gunicorn --bind 127.0.0.1:5000 wsgi:app ,您可以使用 iptables 制定防火墙规则,以将流量从外部接口丢弃到该端口。

如果您使用云提供商,他们可能会在其平台上本地实现此防火墙功能 - 例如,AWS EC2 上的安全组将允许您创建一个“网络服务器”组,该组仅允许端口 80 和 443 的流量通过。< /p>