最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update DNS for domain name in AJAX request - Stack Overflow

programmeradmin0浏览0评论

We have DNS failover setup, with a TTL of 180 seconds. When a server fails, it updates the IP address on the A Host records to that of the working server. Currently, our website refreshes using AJAX requests on an interval. When one of the servers fails, the DNS is updated and failed over, so if you ping the domain it has the proper IP. For some reason though, inside the browser and long after the TTL has expired, the AJAX request still retains the old IP address assigned to the domain name (note the page cannot be reloaded in the browser). I am aware as of reading other questions that you cannot force a flush of the DNS cache on the client. My question is, do any of you know another way or trick to acplish this using javascript or AJAX? Thanks.

We have DNS failover setup, with a TTL of 180 seconds. When a server fails, it updates the IP address on the A Host records to that of the working server. Currently, our website refreshes using AJAX requests on an interval. When one of the servers fails, the DNS is updated and failed over, so if you ping the domain it has the proper IP. For some reason though, inside the browser and long after the TTL has expired, the AJAX request still retains the old IP address assigned to the domain name (note the page cannot be reloaded in the browser). I am aware as of reading other questions that you cannot force a flush of the DNS cache on the client. My question is, do any of you know another way or trick to acplish this using javascript or AJAX? Thanks.

Share Improve this question asked Jun 4, 2014 at 21:36 wayofthefuturewayofthefuture 9,4557 gold badges38 silver badges55 bronze badges 4
  • 2 Looks like an X/Y problem to me; you're only in this situation due to the shortings of your infrastructure ... browsers are notorious for DNS retention, probably for performance reasons, so I wouldn't go down this path unless my job were at stake :) – Ja͢ck Commented Jun 11, 2014 at 14:02
  • It es down to a high availability solution. We have two servers that provide data via ajax request. Since the web app is client-side, it will not be refreshed in the browser. The AJAX refresh will occur on intervals, and the domain name in the ajax will stay the same, however will have an updated IP when a server fails. If there is no way of doing this, do you know how I could redesign the site / ajax request for high availability with two servers handling the request? Thanks – wayofthefuture Commented Jun 11, 2014 at 15:29
  • Just some wishful thinking: if you would do the requests to another server that's always up and never changes their ip and from that server you do a cURL(or something like that) and return the response? So, you use the-always-up-server as some kind of router? – GuyT Commented Jun 18, 2014 at 6:47
  • Hi where would put an always up server ? New York? Western seaboard? China? grafted on to an ocean floor cable? – Old fart Commented Aug 20, 2014 at 9:00
Add a ment  | 

4 Answers 4

Reset to default 4

I would not attempt this client side, I would use a network load balancer, or some other server side virtual IP address.

To do as you request client side, would require that your AJAX calls are made in such a way as to either avoid browser security or fullly conform to cross domain requests. If you are happy to make these changes, then there are some JavaScript DNS libs, these can be used to resolve the DNS name to an IP address or CName, use the returned IP address or CName to adjust your AJAX url, and refresh the AJAX url using a new DNS lookup whenever an AJAX call fails.

I wouldn't want to do this myself

Use two (or more) subdomains. eg. data1.example. and data2.example. that correspond to the physical servers.

However, for cross domain to work, you need to set document.domain which allows the munication between domains:

document.domain = "example.";

Then, you need your script to fail over between the different servers manually. It is the only way that I know of and has been successful in many installs.

I would either:

  • code the ajax to hit server primary.mypany. , if that fails, then switch to secondary.mypany. , if that fails, back to primary.
  • preferably make the failover setup take over a "service ip" from the failed host. There exist good solutions for this.

For linux google stuff like heartbeat and virtual ip and whatnot.

With the browser Firefox, dns.resolve() method can Resolves DNS of given domains.

Reference: https://developer.mozilla/en-US/docs/Mozilla/Add-ons/WebExtensions/API/dns/resolve

发布评论

评论列表(0)

  1. 暂无评论