【问题标题】:Nginx caching DNS look ups and ignoring my resolver settingsNginx 缓存 DNS 查找并忽略我的解析器设置
【发布时间】:2019-06-07 10:32:07
【问题描述】:

我正在使用 docker swarm。我在我的 API 前面放置了一个 nginx 容器用于缓存目的。因为每次我部署我的 API 时,它都会创建一个新的内部 IP,我根据 swarm 文档使用我的服务 tasks.api 的名称。下面是我的位置块

proxy_cache_path /var/cache/nginx/ta_api levels=1:2 keys_zone=api_cache:10m max_size=10g
                  inactive=60m use_temp_path=off;

server {
    listen       80;
    server_name  localhost;

    location / {
      proxy_pass http://tasks.api:10010;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_buffering on;
      proxy_cache api_cache;
      proxy_cache_bypass $http_upgrade;
      # Add header to see if response was cached
      add_header X-Cache-Status $upstream_cache_status;

      # Allow one cache-update request at a time sent to an origin server.
      proxy_cache_lock on;
      # Permit revalidation of stale cached responses.
      proxy_cache_revalidate on;

      # proxy_cache_valid 200 1d;
      # Delivering Cached Content When the Origin is Down
      proxy_cache_use_stale error timeout invalid_header updating
                            http_500 http_502 http_503 http_504;

      # Do all updates in background. With proxy_cache_use_stale, allows stale
      # cached responses to be served.
      proxy_cache_background_update on;
    }
}

我还在我的 http 块中添加了 resolver 127.0.0.11 ipv6=off valid=15s;。但是,当我重新部署我的 API 并获得新的 API 时,nginx 仍会尝试发送到旧 IP。

当我在 nginx 容器上安装绑定工具时,我正在运行一个 nginx 容器标签 nginx:1.15.12-alpine,我可以看到我正在使用 dig tasks.api 获取新 IP

我不知道下一步该尝试什么。我可以硬编码私有 IP,但这不是 docker 方式...

【问题讨论】:

  • 使用旧版链接,所以首先,您需要先启动 tasks.api 容器,然后再启动 nginx,或者将静态 IP 分配给 tasks.api 容器
  • @Adiii Swarm 不使用旧链接,它为您的应用创建用户定义的网络,如 here 所述。这是在Legacy Linking Docker 文档中推荐的。还有其他选择吗?

标签: docker nginx dns


【解决方案1】:

NGINX will only do DNS lookups at startup and cache forever for fixed hostnames。要在运行时启用 DNS 查找,您需要将固定主机名更改为动态变量。所以在OP的情况下,将原来的proxy_pass行改为:

set $target_host tasks.api ;
proxy_pass http://$target_host:10010;

【讨论】:

猜你喜欢
  • 2010-10-12
  • 2018-09-13
  • 2012-11-28
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多