在 https 上运行 Gunicorn?

发布于 2024-12-04 07:11:50 字数 413 浏览 1 评论 0原文

我们有一些通过代理(Apache 和 Nginx)的 Django 设置,最终到达实际的 Django 运行时。

即使 HTTPS 位于我们的网络中,我们也需要端到端的 HTTPS。由于 Gunicorn 在我们其他设置中的成功和性能,我们一直在重新审视它,但需要使用 HTTPS 进行端到端测试以保持一致。

我们的拓扑是这样的:

https://foo.com -> [面向公众的代理]-> (https)-> [内部服务器 https://192...:8001]

如何配置 Gunicorn 来侦听 HTTPS有自签名证书?

We've got a few Django setups that go through a proxy (Apache and Nginx) that eventually make their way to the actual Django runtime.

We need to have HTTPS end to end even once it's in our network. We've been revisiting Gunicorn due to its success and performance in our other setups, but needed to test with HTTPS end to end to be consistent.

Our topology is as such:

https://foo.com -> [Public facing proxy] -> (https) -> [internal server https://192...:8001]

How does one configure Gunicorn to listen on HTTPS with a self signed certificate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日暮斜阳 2024-12-11 07:11:50

Gunicorn 现在支持 SSL,自版本 17.0 起。您可以将其配置为侦听 https,如下所示:

$ gunicorn --certfile=server.crt --keyfile=server.key test:app

如果您使用 --bind 侦听端口 80,请记住将端口更改为 443(HTTPS 连接的默认端口)。例如:

$ gunicorn --certfile=server.crt --keyfile=server.key --bind 0.0.0.0:443 test:app

Gunicorn now supports SSL, as of version 17.0. You can configure it to listen on https like this:

$ gunicorn --certfile=server.crt --keyfile=server.key test:app

If you were using --bind to listen on port 80, remember to change the port to 443 (the default port for HTTPS connections). For example:

$ gunicorn --certfile=server.crt --keyfile=server.key --bind 0.0.0.0:443 test:app
泛滥成性 2024-12-11 07:11:50

回复很晚,但对于遇到此问题的其他人来说,还有另一种选择,使用 nginx 作为上面的“[面向公众的代理]”。

配置 nginx 以处理端口 443 上传入的 SSL 流量,然后将 proxy_pass 发送到内部端口上的 Gunicorn。外部流量是加密的,nginx和gunicorn之间的流量无论如何都不会暴露。我发现这非常容易管理。

Massively late reply, but for anyone else coming across this, there's another option using nginx as the "[Public facing proxy]" above.

Configure nginx to handle the incoming SSL traffic on port 443, and then proxy_pass to gunicorn on an internal port. External traffic is encrypted, and the traffic between nginx and gunicorn isn't exposed anyway. I find this very simple to manage.

殤城〤 2024-12-11 07:11:50

如果您使用gunicorn.config.py或类似的gunicorn配置文件,您可以添加证书文件和密钥文件。

certfile = '/etc/letsencrypt/live/example.com/fullchain.pem'
keyfile = '/etc/letsencrypt/live/example.com/privkey.pem'

配置文件可用于将设置初始化为环境变量,如果您有大量设置,配置文件会很有帮助。
使用配置文件

  • 通过创建名为的文件来创建配置文件
    gunicorn.config.py

  • 一些常用的设置是

    <预><代码>绑定=“0.0.0.0:8000”
    工人 = 4
    pidfile = 'pidfile'
    错误日志 = '错误日志'
    日志级别 = '信息'
    访问日志='访问日志'
    access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(作为”'

    当然

     certfile = '/etc/letsencrypt/live/example.com/fullchain.pem'
      密钥文件 = '/etc/letsencrypt/live/example.com/privkey.pem'
    

文档和配置文件< a href="https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py" rel="noreferrer">示例

这些设置运行gunicorn

    $ gunicorn app:app

使用

默认情况下,将从运行gunicorn 的同一目录中读取名为gunicorn.conf.py 的文件。

If you're using a gunicorn.config.py or similar gunicorn config file you can add the certificate file and key file.

certfile = '/etc/letsencrypt/live/example.com/fullchain.pem'
keyfile = '/etc/letsencrypt/live/example.com/privkey.pem'

Config files can be used to initialise settings as env variables and can be helpful if you had lots of settings.
To use config file

  • Create a config file by creating a file named
    gunicorn.config.py

  • Some usual settings would be

      bind = "0.0.0.0:8000"
      workers = 4
      pidfile = 'pidfile'
      errorlog = 'errorlog'
      loglevel = 'info'
      accesslog = 'accesslog'
      access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
    

    and of course

      certfile = '/etc/letsencrypt/live/example.com/fullchain.pem'
      keyfile = '/etc/letsencrypt/live/example.com/privkey.pem'
    

Check out the documentation and a config file example

to run gunicorn with these settings

    $ gunicorn app:app

since

By default, a file named gunicorn.conf.py will be read from the same directory where gunicorn is being run.

自此以后,行同陌路 2024-12-11 07:11:50

除了certfilekeyfile之外,您还需要添加ca-certs。在没有通过ca-certs的情况下,我在Android设备上获得了未找到证书路径的信任锚。

命令示例:

/usr/bin/python3 /usr/local/bin/gunicorn --bind 0.0.0.0:443 wsgi:app --workers=8 --access-logfile=/root/app/logs/access.log --error-logfile=/root/app/logs/error.log --certfile=/root/app/certificate.crt --keyfile=/root/app/private.key --ca-certs=/root/app/ca_bundle.crt --daemon

In addition to certfile and keyfile you need to add ca-certs as well. Without passing ca-certs, I was getting Trust anchor for certification path not found. on Android devices.

Sample command:

/usr/bin/python3 /usr/local/bin/gunicorn --bind 0.0.0.0:443 wsgi:app --workers=8 --access-logfile=/root/app/logs/access.log --error-logfile=/root/app/logs/error.log --certfile=/root/app/certificate.crt --keyfile=/root/app/private.key --ca-certs=/root/app/ca_bundle.crt --daemon
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文