【发布时间】:2019-10-02 09:02:22
【问题描述】:
我在 AWS 中有 nginx 容器,它为我的网站做反向代理,例如https://example.com。我有自动在本地 DNS 中注册的后端服务 - aws.local(这是由 AWS ECS Auto-Discovery 完成的)。 我遇到的问题是 nginx 仅在启动期间将名称解析为 IP,因此当服务容器重新启动并获取新 IP 时,nginx 仍然尝试旧 IP,并且出现“502 Bad Gateway”错误。
这是我正在运行的代码:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
include /etc/nginx/mime.types;
log_format graylog2_json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
upstream service1 {
server service1.aws.local:8070;
}
upstream service2 {
server service2.aws.local:8080;
}
resolver 10.0.0.2 valid=10s;
server {
listen 443 http2 ssl;
server_name example.com;
location /main {
proxy_pass http://service1;
}
location /auth {
proxy_pass http://service2;
}
我找到了更改 nginx 配置以解析每个请求的名称的建议,但随后我看到我的浏览器尝试打开“service2.aws.local:8070”,但由于其 AWS 本地 DNS 名称而失败。我应该在我的浏览器上看到https://example.com/auth"。
server {
set $main service1.aws.local:2000;
set $auth service2.aws.local:8070;
location /main {
proxy_http_version 1.1;
proxy_pass http://$main;
}
location /auth {
proxy_http_version 1.1;
proxy_pass http://$auth;
}
你能帮我修一下吗? 谢谢!!!
【问题讨论】:
标签: amazon-web-services docker nginx amazon-ecs nginx-reverse-proxy