设置cURL以使用本地虚拟主机 [英] Set cURL to use local virtual hosts

查看:229
本文介绍了设置cURL以使用本地虚拟主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Apache或Ngnix我总是基于真实项目创建开发网站,例如 http://project1.loc ,在添加到我的

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, the browser has no problem using.

但是,当我尝试创建一个cURL请求( http://project1.loc/post.json )到同一个URL我从来没有得到任何,但超时。我假设cURL不关心我的自定义主机,并直接到名称服务器的信息。

However, when I attempt to make a cURL request (http://project1.loc/post.json) to that same URL I never get anything but a timeout. I'm assuming cURL doesn't care about my custom hosts and goes straight to a name server for it's info.

如何解决这个问题?

UPDATE
我设置了自定义标题HOST: http ://project1.loc 现在我得到400错误 - 但他们是即时的,所以我假设cURL至少使用主机文件...

UPDATE I set a custom header "HOST: http://project1.loc" and now I am getting 400 errors - but they are instantaneous so I'm assuming that cURL is at least using the hosts file...

推荐答案

编辑:虽然这是目前被接受的答案,读者可能会发现此用户约翰·哈特更适合他们的需要。它使用一个选项,根据用户 Ken ,在版本7.21.3中引入(这是发布于2010年12月,即此初始回答后)。

While this is currently accepted answer, readers might find this other answer by user John Hart more adapted to their needs. It uses an option which, according to user Ken, was introduced in version 7.21.3 (which was released in December 2010, i.e. after this initial answer).

在您编辑的问题中,您使用的是网址作为主机名,而只需要是主机名。

In your edited question, you're using the URL as the host name, whereas it needs to be the host name only.

尝试:

curl -H 'Host: project1.loc' http://127.0.0.1/something

其中 project1.loc

(如果您的主机名是,则 127.0.0.1 从库中而不是在命令行中使用curl,请确保不要在主机 http:// $ c> header。)

(If you're using curl from a library and not on the command line, make sure you don't put http:// in the Host header.)

这篇关于设置cURL以使用本地虚拟主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆