I am using docker swarm. I placed an nginx container in front of my API for caching purposes. Since every time I deploy my API it creates a new internal IP I am using the name of my service tasks.api as per swarm documentation. Below is my location block
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;
}
}
I also added resolver 127.0.0.11 ipv6=off valid=15s; to my http block. However, when I redeploy my API and it gets a new API nginx still attempts to send to the old IP.
I am running an nginx container tag nginx:1.15.12-alpine when I install bind-tools on the nginx container I can see that I am getting the new IPs using dig tasks.api
I am at a loss what to try next. I can hardcode private IPs, but that is not the docker way...
NGINX will only do DNS lookups at startup and cache forever for fixed hostnames. To enable DNS lookup during runtime, you need to change the fixed hostname to a dynamic variable. So in the OP case, change the original proxy_pass line to:
set $target_host tasks.api ;
proxy_pass http://$target_host:10010;