hey*_*hey 5 domain-name-system nginx reverse-proxy load-balancing amazon-web-services
我们今天在 AWS 环境中遇到了 nginx 解析器的问题。根据 nginx 日志文件,在 DNS 记录更改后,nginx 没有解析到正确的服务器。
DNS最初解析:
webservers.ourservers.local A 10.0.0.5
webservers.ourservers.local A 10.0.1.8
Run Code Online (Sandbox Code Playgroud)
...但是一旦这些 IP 地址之一发生更改,nginx 就没有得到更改 - 并不断尝试访问过时的 IP 地址 - 远长于 60 秒 ttl。
根据 nginx.org/en/docs/http/ngx_http_core_module.html#resolver,nginx 解析器仅缓存 ttl 的答案 - 或者,在我们的例子中,为配置的时间。
我的同事将resolver线路从位置块移动到服务器块,但它没有帮助。我们需要重新启动 nginx 以强制它解析到新的 ip。
nginx配置:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$scheme - $http_x_client_ip - $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;
error_log /var/log/nginx/error.log;
server {
listen 8080 default;
set $OUR_ROOT /var/www/pub;
location /health_check.html {
root $OUR_ROOT;
auth_basic off;
allow all;
}
location / {
# Work around to flush Nginx: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver
# https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html#AmazonDNS
# This is will be your VPC's IPv4 CIDR value + 2
# You can find this value in /etc/resolv.conf on the EC2 instance
resolver 10.0.0.2 valid=15s;
proxy_pass http://webservers.ourservers.local; # Will be resolved by Route 53
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Proto-New https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Host $host;
proxy_redirect off;
}
}
#include custom config
include /etc/nginx/conf.d/*.conf;
}
Run Code Online (Sandbox Code Playgroud)
我们使用 nginx 版本 1.13.7。
我们做错了什么?我们如何让 nginx 重新获取 dns 记录?
更新/澄清:
10.0.0.2 是 VPC 的 AWS 内部名称服务器:https : //docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html#AmazonDNS。通过检查它确实可以正确解析dig,即使 nginx 不能同时正确解析:我们已经测试了这些情况
dig webservers.ourservers.local @10.0.0.2
Run Code Online (Sandbox Code Playgroud)