使用 Apache 或 Ngnix,我总是基于真实项目(例如 http://project1.loc
)创建开发站点,在添加到我的 .hosts
文件后,浏览器没有使用时出现问题。
但是,当我尝试向同一 URL 发出 cURL 请求 (http://project1.loc/post.json
) 时,除了超时之外,我什么也得不到。我假设 cURL 不关心我的自定义主机,而是直接访问名称服务器以获取其信息。
我该如何解决这个问题?
更新
我设置了一个自定义标头“HOST: http://project1.loc”,现在我收到 400 个错误 - 但它们是瞬时所以我假设 cURL 至少正在使用主机文件......
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.
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.
How can I fix this?
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...
发布评论
评论(7)
实际上,curl 有一个明确的选项:
--resolve
--resolve
而不是curl -H 'Host: yada.com' http://127.0.0.1/something
使用 code>curl --resolve 'yada.com:80:127.0.0.1' http://yada.com/something
你问有什么区别吗?
其中,这适用于 HTTPS。假设您的本地服务器有
yada.com
的证书,上面的第一个示例将失败,因为yada.com
证书与127.0.0.1< /code> URL 中的主机名。
第二个示例可以正确使用 HTTPS。
本质上,通过
-H
传递“Host”标头确实会将您的主机侵入标头集,但绕过了所有curl 的特定于主机的智能。使用--resolve
利用所有适用的正常逻辑,但只是假装 DNS 查找在命令行选项中返回了数据。它的工作原理就像/etc/hosts
应该的那样。注意
--resolve
需要一个端口号,因此对于 HTTPS,您可以使用curl --resolve 'yada.com:443:127.0.0.1' https://yada.com/something< /代码>
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 theyada.com
certificate doesn't match the127.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 usecurl --resolve 'yada.com:443:127.0.0.1' https://yada.com/something
编辑:虽然这是当前接受的答案,但读者可能会发现用户这个其他答案 href="https://stackoverflow.com/users/260371/john-hart">John Hart 更适合他们的需求。根据用户 Ken 的说法,它使用的选项是在版本 7.21.3 中引入的(2010 年 12 月发布,即在此初始答案之后)。
在您编辑的问题中,您使用 URL 作为主机名,而它只需是主机名。
尝试:
其中
project1.loc
只是主机名,127.0.0.1
是目标 IP 地址。(如果您使用库中的curl而不是在命令行上使用,请确保不要将
http://
放入Host
标头中。)EDIT: 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.
Try:
where
project1.loc
is just the host name and127.0.0.1
is the target IP address.(If you're using curl from a library and not on the command line, make sure you don't put
http://
in theHost
header.)为了在尚未通过 DNS 连接的 Apache http 服务器上设置虚拟主机,我喜欢使用:
其中主机名是运行 Web 服务器的计算机的 IP 地址或 DNS 名称。
这也适用于 https 站点。
For setting up virtual hosts on Apache http-servers that are not yet connected via DNS, I like to use:
Where host-name ist the IP address or the DNS name of the machine on which the web-server is running.
This also works well for https-Sites.
使用指向
127.0.0.1
的真实完全限定域名(例如dev.yourdomain.com
),或者尝试编辑正确的主机文件(通常是 /etc/hosts *nix 环境)。Either use a real fully qualified domain name (like
dev.yourdomain.com
) that pointing to127.0.0.1
or try editing the proper hosts file (usually /etc/hosts in *nix environments).看来这并不是一个罕见的问题。
首先检查此。
如果这不起作用,您可以在 Windows 上安装本地 DNS 服务器,例如此。配置 Windows 以使用 localhost 作为 DNS 服务器。该服务器可以配置为对您需要的任何假域名具有权威性,并将所有其他请求的请求转发到真实的 DNS 服务器。
我个人认为这有点过头了,不明白为什么主机文件不起作用。但它应该可以解决你遇到的问题。确保您也将常规 DNS 服务器设置为转发器。
It seems that this is not an uncommon problem.
Check this first.
If that doesn't help, you can install a local DNS server on Windows, such as this. Configure Windows to use localhost as the DNS server. This server can be configured to be authoritative for whatever fake domains you need, and to forward requests on to the real DNS servers for all other requests.
I personally think this is a bit over the top, and can't see why the hosts file wouldn't work. But it should solve the problem you're having. Make sure you set up your normal DNS servers as forwarders as well.
服务器是否真正收到请求,并且您是否正确处理主机名(别名)?
检查您的网络服务器日志,看看请求是如何进来的...
curl 有选项来转储发送的请求和收到的响应,这称为跟踪,它将保存到文件中。
--trace
如果您缺少主机或标头信息 - 您可以使用配置选项强制这些标头。
我会让curl 请求在命令行上运行,然后尝试在PHP 中实现。
配置选项是
-K/--config
与curl相关的选项在这里
--trace
启用所有传入和传出数据(包括描述信息)的完整跟踪转储到给定的输出文件。使用“-”作为文件名将输出发送到标准输出。
-K/--配置
指定从哪个配置文件读取curl参数。配置文件是一个文本文件,可以在其中写入命令行参数,然后将其使用,就好像它们是在实际中写入的一样
命令行。选项及其参数必须在同一配置文件行上指定,并用空格、冒号、等号或其任意组合分隔(但是,首选分隔符
tor 是等号)。如果参数要包含空格,则必须将参数括在引号内。在双引号内,可以使用以下转义序列:\、\"、\t、\n、
\r 和 \v。任何其他字母之前的反斜杠将被忽略。如果配置行的第一列是“#”字符,则该行的其余部分将被视为注释。每一项只写一个选项
配置文件中的物理行。
Does the server actually get the requests, and are you handling the host name (alias) properly?
Check your webserver log, to see how the request came in...
curl has options to dump the request sent, and response received, it is called trace, which will will be saved to a file.
--trace
If you are missing host or header information - you can force those headers with the config option.
I would get the curl request working on the command line, and then try to implement in PHP.
the config option is
-K/--config
the options that are relevant in curl are here
--trace
Enables a full trace dump of all incoming and outgoing data, including descriptive information, to the given output file. Use "-" as filename to have the output sent to stdout.
-K/--config
Specify which config file to read curl arguments from. The config file is a text file in which command line arguments can be written which then will be used as if they were written on the actual
command line. Options and their parameters must be specified on the same config file line, separated by whitespace, colon, the equals sign or any combination thereof (however, the preferred separa-
tor is the equals sign). If the parameter is to contain whitespace, the parameter must be enclosed within quotes. Within double quotes, the following escape sequences are available: \, \", \t, \n,
\r and \v. A backslash preceding any other letter is ignored. If the first column of a config line is a '#' character, the rest of the line will be treated as a comment. Only write one option per
physical line in the config file.
发出请求
向
-H
日志文件,其中包含:我的主机文件如下所示:
Making a request to
Resulted in the
-H
log file containing:My hosts file looks like: