问题
I have a fairly complex haproxy configuration that routes to backends based on the host of the request (via acl + hdr_dom). If I want to test the configuration locally, I have to change the resolution of the hosts I want to test (for example, by changing hosts file to resolve host to 127.0.0.1). I can then use wget or curl to test the haproxy configuration locally, and reset the hosts file afterward.
However, I want to have automated tests, and changing system level things in an automated test makes me nervous. Before I go down the path of changing the hosts file in an automatic way, is there a way to tell haproxy "pretend this request was made to xxx.yyy.com"? Is there some other approach I can take?
Thanks
回答1:
is there a way to tell haproxy "pretend this request was made to xxx.yyy.com"?
I believe that's the wrong question. The correct question is "How can I tell curl that example.com's IP address is 127.0.0.1?"
The answer is --resolve
.
curl -v --resolve example.com:80:127.0.0.1 http://example.com
curl -v --resolve example.com:443:127.0.0.1 https://example.com
This preloads the answer (127.0.0.1) into curl's DNS cache for that invocation only, which apparently tricks it into not actually doing a DNS lookup, because it thinks it already did. Instead, it will connect to the IP address you specify.
回答2:
The answer from Micheal above is the best way to approach this (as it actually goes through the DNS lookup). If that doesn't work, I found that setting the Host HTTP Header to the server also works. For example, in Windows/PowerShell the following works.
iwr http://localhost:8090/testjudge.aspx?version=1 -headers @{Host="xxx.yyy.com"}
Linux's native curl had a different syntax, but the idea is the same. See This post on how to do this on various platforms
来源:https://stackoverflow.com/questions/46354541/how-do-i-locally-test-haproxy-when-routing-based-on-host-name