如何解决 Elastic Load Balancer 的 502 Bad Gateway 错误和 HTTPS 请求的 EC2/Nginx?
How to solve 502 Bad Gateway errors with Elastic Load Balancer and EC2/Nginx for HTTPS requests?
在 EC2 实例 运行 Nginx 前使用 AWS Elastic Load Balancer(应用程序类型)时,我 运行 遇到 HTTPS 请求的“502 错误网关”问题。 Nginx 在服务于 python 应用程序(金字塔框架)的女服务员服务器的每个实例上充当反向代理。我正在尝试在 ELB 上使用 TLS 终止,以便 EC2 实例仅处理 HTTP。这是粗略的设置:
客户端 HTTPS 请求 > ELB(侦听 443,在后端转发到 80)> Nginx 侦听端口 80(在 Ec2 实例上)> 转发到 waitress/Pyramid(在同一个 ec2 实例上)
当我在 HTTPS 上发出请求时,出现 502 错误。但是,当我发出常规 HTTP 请求时,我得到了预期的响应(与上面相同的设置,除了 ELB 正在侦听端口 80)。
一些附加信息:
ELB 健康检查正在运行。
所有 VPC/Security 组都配置正确(我相信)。
我在 ELB 上使用 AWS 证书,使用 AWS 上的标准 setup/walkthrough。
我通过 SSH 连接到 Ec2 实例,在 Nginx 访问日志中,HTTPS 请求似乎仍处于加密状态?或者一些编码问题?
这是 EC2 实例上的 nginx.conf:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /etc/nginx/access.log;
sendfile on;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:6543;
}
server {
listen 80;
server_name [MY-EC2-SERVER-NAME];
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
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;
}
}
}
好的,我明白了(我是个傻瓜)。我在 ELB 上设置了两个监听器,一个用于 80,一个用于 443,这是正确的。 80 的侦听器已正确设置为按预期通过 HTTP 转发到后端 (Nginx) 端口 80。 443 侦听器被错误地配置为通过 HTTPS 发送到后端的端口 80。我更新了 443 侦听器以使用与 80 侦听器相同的规则(即侦听 443 但通过 HTTP 发送到后端 80)并且它有效。无视你们。
在 EC2 实例 运行 Nginx 前使用 AWS Elastic Load Balancer(应用程序类型)时,我 运行 遇到 HTTPS 请求的“502 错误网关”问题。 Nginx 在服务于 python 应用程序(金字塔框架)的女服务员服务器的每个实例上充当反向代理。我正在尝试在 ELB 上使用 TLS 终止,以便 EC2 实例仅处理 HTTP。这是粗略的设置:
客户端 HTTPS 请求 > ELB(侦听 443,在后端转发到 80)> Nginx 侦听端口 80(在 Ec2 实例上)> 转发到 waitress/Pyramid(在同一个 ec2 实例上)
当我在 HTTPS 上发出请求时,出现 502 错误。但是,当我发出常规 HTTP 请求时,我得到了预期的响应(与上面相同的设置,除了 ELB 正在侦听端口 80)。
一些附加信息: ELB 健康检查正在运行。 所有 VPC/Security 组都配置正确(我相信)。 我在 ELB 上使用 AWS 证书,使用 AWS 上的标准 setup/walkthrough。 我通过 SSH 连接到 Ec2 实例,在 Nginx 访问日志中,HTTPS 请求似乎仍处于加密状态?或者一些编码问题?
这是 EC2 实例上的 nginx.conf:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /etc/nginx/access.log;
sendfile on;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:6543;
}
server {
listen 80;
server_name [MY-EC2-SERVER-NAME];
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
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;
}
}
}
好的,我明白了(我是个傻瓜)。我在 ELB 上设置了两个监听器,一个用于 80,一个用于 443,这是正确的。 80 的侦听器已正确设置为按预期通过 HTTP 转发到后端 (Nginx) 端口 80。 443 侦听器被错误地配置为通过 HTTPS 发送到后端的端口 80。我更新了 443 侦听器以使用与 80 侦听器相同的规则(即侦听 443 但通过 HTTP 发送到后端 80)并且它有效。无视你们。