【发布时间】: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 文档中推荐的。还有其他选择吗?