【问题标题】:AWS loadbalancer + Nginx + gunicorn - how to use sslAWS 负载均衡器 + Nginx + gunicorn - 如何使用 ssl
【发布时间】:2019-04-10 17:25:44
【问题描述】:

我正在尝试使用 docker-machine 使用堆栈 nginx+gunicorn+django 创建 ec2 实例。我有一个面向 Internet 的应用程序负载均衡器,它有两个目标组(绿色/蓝色),以防止新部署中的任何停机(新部署被部署到未使用的组,并且在一切检查完毕时负载均衡器组发生更改)

在端口 80 上一切正常,但是当我尝试为端口 443 添加一个监听器并为该域提供亚马逊颁发的证书时,我无法让它工作。我还需要从 Nginx 内部获取证书吗?我需要 Nginx 来监听 443 端口吗?目标?

负载均衡器:

+------------+-----------------------------+----------------+
| Listener ID|            Rules.           | SSL Certificate|
+------------+-----------------------------+----------------+
| HTTP: 80   | Default: forwarding to blue.| N/A.           |
+------------+-----------------------------+----------------+
| HTTPS: 443 | Default: forwarding to blue.|Default: (ACM). |
+------------+-----------------------------+----------------+

目标:

+------------+--------------+--------------+--------------+---------------+
|    Name    |     Port     |   Protocol   |  Target type | Load Balancer |
+------------+--------------+--------------+--------------+---------------+
| Blue       | 80           | HTTP         | instance     | loadbalancer  |
+------------+--------------+--------------+--------------+---------------+
| Green      | 80           | HTTP         | instance     |               |
+------------+--------------+--------------+--------------+---------------+

我的 Nginx 配置:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  #tcp_nopush     on;

  keepalive_timeout  65;

  upstream app {
    server django:5000;
  }

  server {
    listen 80;
    charset utf-8;

    location = /favicon.ico {
        return 204;
        access_log     off;
        log_not_found  off;
    }

    location / {
        try_files $uri @proxy_to_app;
    }

    # django app
     location @proxy_to_app {
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_pass http://app;
    }
  }
}

【问题讨论】:

  • 如果是Elastic Load Balancer,需要安装SSL证书。
  • 是的,负载均衡器安装了 Amazon 颁发的 SSL 证书,并在 443 的端口侦听器上使用。
  • 您看到了什么确切的错误?您在哪里看到交通正在停止?在 ALB 端或 Nginx 级别?您可以让 ALB 在 443 上侦听并进行 SSL 终止并在 80 上执行后端 Nginx。
  • 哇,愚蠢的错误! docker-machine 使用的我的安全组不会自动允许端口 443,因为它没有在 docker-compose 中指定。添加规则,现在一切正常!
  • 哦。高兴的。解决了!!

标签: amazon-web-services nginx


【解决方案1】:

如果其他人遇到此问题,请写我自己的答案。

用于创建 Ec2 实例的安全组需要打开传入端口 443。如果您的 docker-compose 文件没有公开端口 443,它不会自动将端口添加为安全组上的打开。您可以在网络和安全下的 Ec2 的 aws 控制台中手动打开端口。

【讨论】:

  • gunicorn 默认监听 800 端口。那么你如何设置 gunicon 在 ssl 上收听?
  • 将 gunicorn 直接暴露在互联网上并不是一个好主意。在它前面使用反向代理,比如 nginx 或 traefik。
  • 我明白,但是我现在已经让 apache 监听端口 443 并代理到端口 8000 上的 gunicorn。但我似乎无法通过弹性豆茎使其工作
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2015-01-13
  • 2016-05-12
  • 1970-01-01
  • 2016-11-10
  • 2020-12-28
  • 1970-01-01
相关资源
最近更新 更多