【问题标题】:ELB redirecting 443/80 to single port on nginx docker containerELB 将 443/80 重定向到 nginx docker 容器上的单个端口
【发布时间】:2017-01-29 20:24:57
【问题描述】:

我们有一个公共 AWS ELB,它像这样重定向流量:

HTTP    80  HTTP    9001
TCP     443 TCP     9001

目标实例是使用 nginx 容器运行 docker 的 AWS ECS 实例。

Docker 正在转发 9001 -> 8080,而 nginx 正在监听 8080。 这是 nginx 配置的片段:

server {
    ssl on;
    ssl_certificate /etc/nginx/mydomain.crt;
    ssl_certificate_key /etc/nginx/mydomain.key;

    listen 8080;
    server_name %{ROUTER_CLEARCARE_SERVER_NAME};

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


    if ($http_x_forwarded_proto != 'https') {
        return 301 https://$host$request_uri;
    }

    set $target_web "web.mydomain.com:80";
    location / {
        proxy_read_timeout 180;
        proxy_connect_timeout 2;
        proxy_send_timeout 180;
        keepalive_timeout  180;
        resolver 10.10.0.2 valid=30s;
        proxy_set_header Host $host;
        proxy_pass http://$target_web;
        proxy_set_header X-Unique-ID $request_id;
    }
}

我需要在 nginx 容器上进行 SSL 终止,因为我们有多个域的多个证书,并且我们使用具有不同超时时间的基于路径的路由(ELB 仅支持单个证书,而 ALB 不支持基于路径的路由不同超时和证书)。

这里是关键:nginx 只能监听一个端口(我们正在使用一个名为 Empire 的工具将 nginx 容器部署到 AWS ECS,他们目前只支持这种配置)。

nginx 可以在单个端口上支持 http 和 https 吗?

现在,使用该配置,我在尝试点击 http://example.com 时收到此错误:

The plain HTTP request was sent to HTTPS port

当我尝试点击 https://example.com 时出现此错误,我收到此错误:

mydomain.com redirected you too many times.

【问题讨论】:

    标签: nginx amazon-elb


    【解决方案1】:

    我发现了一个声明,在this serverfault page (check out 2nd answer from Komu) 上的 NginX 应该可以监听 HTTP 和 HTTPS。我在下面重复它,这样你就可以更容易地找到它。你能试试吗?如果你没有绑定 NginX,你可能也会对this node.js plugin 感兴趣,它还允许在同一个端口上监听 HTTP 和 HTTPS。


    引自here

    根据维基百科关于状态码的文章,Nginx 有一个自定义 http流量发送到https端口时的错误码(错误码497)

    根据 error_page 上的 nginx 文档,您可以定义一个 URI 将针对特定错误显示。因此我们可以创建一个 uri 当出现错误代码 497 时,客户端将被发送到。

    #lets assume your IP address is 89.89.89.89 and also that you want nginx to listen on port 7000 and your app is running on port 3000
    
    server {
        listen 7000 ssl;
    
        ssl_certificate /path/to/ssl_certificate.cer;
        ssl_certificate_key /path/to/ssl_certificate_key.key;
        ssl_client_certificate /path/to/ssl_client_certificate.cer;
    
        error_page 497 301 =307 https://89.89.89.89:7000$request_uri;
    
        location / {
            proxy_pass http://89.89.89.89:3000/;
    
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Protocol $scheme;
        }
    }
    

    但是,如果客户端通过 GET 以外的任何其他方法发出请求, 该请求将被转换为 GET。从而保留请求 客户进来的方法;我们使用错误处理重定向 如 error_page 上的 nginx 文档所示

    这就是我们使用 301 =307 重定向的原因。

    使用这里显示的 nginx.conf 文件,我们可以拥有 http 和 https监听同一个端口

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2018-01-25
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2019-02-02
      • 2018-04-30
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多