Prevent nginx from caching DNS for proxy

Hello everyone,

If you are using nginx as a proxy and/or reverse proxy, the nginx is caching the DNS information and if you are using AWS Application Load Balancer behind the nginx, and nginx sometimes needs to restart and/or DNS flushing to send a request to the AWS Application Load Balancer because AWS is always giving a CNAME and changing the Load Balancer IP address frequently.

There is no option to flush DNS on nginx, the only option is to restart the nginx and you can’t solve the problems with restart always : ) You can use the nginx config below to fix this DNS Caching problem.

server {
   listen       80;
   server_name  proxy-request.ercanermis.net;

   include /etc/nginx/default.d/*.conf;

   set $proxydestination "backend.ercanermis.com";
   proxy_pass https://$proxydestination;
   proxy_http_version 1.1;
   proxy_set_header Host proxy-request.ercanermis.net;
   proxy_redirect off;
   proxy_cache_bypass 1;
   proxy_no_cache 1;
   resolver 8.8.8.8 valid=5s;
   }
}

If you have any questions, please feel free to comment, thank you!