Set cURL to use local virtual hosts

后端 未结 7 1618
囚心锁ツ
囚心锁ツ 2020-11-28 01:37

Using Apache or Ngnix I always create development sites based on real projects such as http://project1.loc which, after adding to my .hosts file, t

相关标签:
7条回答
  • 2020-11-28 02:10

    Actually, curl has an option explicitly for this: --resolve

    Instead of curl -H 'Host: yada.com' http://127.0.0.1/something

    use curl --resolve 'yada.com:80:127.0.0.1' http://yada.com/something

    What's the difference, you ask?

    Among others, this works with HTTPS. Assuming your local server has a certificate for yada.com, the first example above will fail because the yada.com certificate doesn't match the 127.0.0.1 hostname in the URL.

    The second example works correctly with HTTPS.

    In essence, passing a "Host" header via -H does hack your Host into the header set, but bypasses all of curl's host-specific intelligence. Using --resolve leverages all of the normal logic that applies, but simply pretends the DNS lookup returned the data in your command-line option. It works just like /etc/hosts should.

    Note --resolve takes a port number, so for HTTPS you would use

    curl --resolve 'yada.com:443:127.0.0.1' https://yada.com/something

    0 讨论(0)
提交回复
热议问题