11

Say you have Server A set up with your favourite webserver-daemon to serve www.example.com. Now you want to move this to Server B. After a bunch of copying and configuring, the new server seems to be ready. A final test would be in order: Before changing the DNS-records, how does one ask for www.example.com from Server B when the DNS records still point to server A?


While my main question has been answered, in the event of /etc/hosts not being under my realm of influence, is it possible to test it with telnet by instead talking raw HTTP1.1 to the webserver?

Jarmund
  • 535
  • 1
  • 6
  • 16
  • 8
    Please don't edit your question to ask a completely new question. It tends to (somewhat) invalidate existing answers and also the platform does not send notifications that a question has changed either, making it unlikely that people already involved with your question will revisit their answers. You could leave a direct comment for them that you have a follow up question. Often a better strategy is to ask a new question (possibly pointing to your earlier Q&A) instead. – HBruijn Dec 27 '15 at 10:18
  • 2
    Highly considering down voting just for the complete goalpost shift with the edit. >_ – Wesley Dec 27 '15 at 14:47

5 Answers5

12

DNS is a magical thing. With enough control over a client PC, you can make right.com into wrong.com and viceversa.io. Jump onto a test client PC and muck with its DNS resolution by either changing the host file on that client, or you can do something a bit more complex and give that host a simple DNS server to query, like dnsmasq. Either which way you go about the process, the ultimate goal is to make the DNS response for queries concerning www.example.com respond with the IP address for Server B. HTTP host headers will then bear the DNS name for www.example.com, but be aimed at Server B.

Magic!

Wesley
  • 32,320
  • 9
  • 80
  • 116
  • 3
    Somehow `/etc/hosts` didn't occur to me, and this worked perfectly. Cheers for the reminder! – Jarmund Dec 27 '15 at 01:48
  • 2
    The `hosts` file is not involved in *DNS resolution* but is an entirely separate name lookup mechanism that the resolver library provided by the client OS may, and in most cases will, use in addition to lookups in DNS. Personally I think the "it's magic" bit just feeds into "there's no need to understand", which is certainly a bad thing for professionals. – Håkan Lindqvist Dec 27 '15 at 17:05
  • @HåkanLindqvist You have sorely misunderstood the context and use of the phrase. – Wesley Dec 27 '15 at 19:42
  • In any case, [hosts file](https://en.wikipedia.org/wiki/Hosts_%28file%29) ≠ [DNS](https://en.wikipedia.org/wiki/Domain_Name_System). This is SF; I'd expect people here, if anywhere, to know the difference. – Ilmari Karonen Dec 28 '15 at 12:07
  • @IlmariKaronen And I didn't say that hosts files were DNS, so your expectations for ServerFault have been met and we're all good here! ᕕ( ᐛ )ᕗ – Wesley Dec 28 '15 at 15:02
11

It's important to note that what matters to do a test like this for HTTP is not really related to DNS at all but what the HTTP client sends as the Host header value.

The client needs to connect to the right IP address and port, obviously, but beyond that it all comes down to the Host header which is inline in the HTTP request itself.


For a quick test the following command can be used without changing any operating system level configuration on the client:

$ curl -H "Host: www.example.com" http://192.0.2.17/foo/bar

or, for that matter,

$ curl -H "Host: www.example.com" http://beta.example.com/foo/bar


To do the same with a regular web browser you would either need a browser extension that can modify the request headers (or possibly an HTTP proxy that changes headers), or you would need to change the configuration of the client machine operating system.

As has been mentioned, the easiest means of doing such a configuration change would be to add an entry to the hosts file. This way the client OS resolver library would get a hit already when consulting the hosts file and will not even need to do a DNS lookup.

Other options include changing the configured DNS resolver servers on the client to a nameserver which is set up to answer as desired.

Håkan Lindqvist
  • 33,741
  • 5
  • 65
  • 90
  • 1
    +1 for a solution that does not involve mucking around with name resolution – Jarmund Dec 27 '15 at 22:56
  • 2
    Recent `curl` allows you to override normal name resolution (DNS *or* hostfile) and still do headers automatically, and also SNI if using/testing httpS. See `--resolve` in your manpage or at http://curl.haxx.se/docs/manpage.html . – dave_thompson_085 Dec 28 '15 at 09:53
4

You can test this with telnet. You need to make a raw HTTP request by typing the commands. You can't make any mistakes, and some servers have a timeout so you also have to type the whole request within that time frame.

The command will be something like this:

telnet serverb 80

The request will look something like this:

GET / HTTP/1.1
Host: www.example.com

Note that it must end with a blank line, so you'd press enter twice after the host header.

briantist
  • 2,535
  • 18
  • 34
  • 1
    Nitpick: while HTTP headers are not supposed to be case-sensitive, the [normative `Host` header](https://tools.ietf.org/html/rfc7230#section-5.4) is not all-caps. (And apparently [some servers](http://stackoverflow.com/a/34039108) might have trouble with different capitalisations.) – Bob Dec 27 '15 at 10:14
  • @Bob true, edited – briantist Dec 27 '15 at 16:43
3

What I usually do is force www.example.com to server B IP address in my /etc/hosts. I've been doing that since I used Mosaic and since I do it infrequently I haven't felt the need to find better.

Looking around a bit for you, I found https://superuser.com/questions/403042/custom-host-file-for-firefox which is answered by https://addons.mozilla.org/es/firefox/addon/foxyproxy-standard/ but there is a very recent note saying that it is abandoned.

Law29
  • 3,507
  • 1
  • 15
  • 28
  • Responding to your edit asking for a testing method without /etc/hosts, yes, that exists. It would be either telnet for very basic testing, an addon such as I mentioned in my answer above, or a custom-configured proxy, or (and this is the method I would prefer) a ServerAlias on server B of say www2.example.com. – Law29 Dec 27 '15 at 12:39
3

If you need to check if it works, I advice a simple broswer plugin like Modify Headers for Firefox, and change the HOST to www.mydomain.com.

bukk530
  • 141
  • 3