将cURL设置为使用本地虚拟主机

时间:2010-08-02 18:14:59

标签: curl hosts

使用Apache或Ngnix我总是根据http://project1.loc这样的实际项目创建开发站点,在添加到我的.hosts文件后,浏览器没有问题。

然而,当我尝试向同一个URL发出cURL请求(http://project1.loc/post.json)时,除了超时之外我什么也得不到。我假设cURL不关心我的自定义主机,直接进入名称服务器获取它的信息。

我该如何解决这个问题?

更新 我设置了一个自定义标题“HOST:http://project1.loc”,现在我收到400个错误 - 但它们是瞬间的,所以我假设cURL至少使用了hosts文件...

7 个答案:

答案 0 :(得分:372)

实际上,curl有一个明确的选项:--resolve

而不是curl -H 'Host: yada.com' http://127.0.0.1/something

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

你问的有什么区别?

其中,这适用于HTTPS。假设您的本地服务器具有yada.com证书,则上面的第一个示例将失败,因为yada.com证书与URL中的127.0.0.1主机名不匹配。

第二个示例适用于HTTPS。

实质上,通过-H传递“主机”标头会将您的主机入侵到标头集,但会绕过所有curl的主机特定情报。使用--resolve利用所有适用的常规逻辑,但只是假装DNS查找返回命令行选项中的数据。它就像/etc/hosts应该的那样工作。

注意--resolve采用端口号,因此对于HTTPS,您将使用

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

答案 1 :(得分:119)

编辑:虽然这是目前接受的答案,但读者可能会发现用户this other answerJohn Hart更适合他们的需求。它根据用户Ken使用了一个选项,该版本在版本7.21.3中引入(released in December 2010,即在此初始答案之后)。


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

尝试:

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

其中project1.loc 只是主机名,127.0.0.1是目标IP地址。

(如果您使用的是库中的curl而不是命令行,请确保不要将http://放在Host标题中。)

答案 2 :(得分:2)

使用指向dev.yourdomain.com的真实完全限定域名(如127.0.0.1)或尝试编辑正确的主机文件(通常在* nix环境中使用/ etc / hosts)。

答案 3 :(得分:2)

这似乎不是一个罕见的问题。

先检查this

如果这样做没有帮助,您可以在Windows上安装本地DNS服务器,例如this。配置Windows以使用localhost作为DNS服务器。可以将此服务器配置为对您需要的任何虚假域具有权威性,并将请求转发到真实DNS服务器以用于所有其他请求。

我个人认为这有点超过顶部,并且无法理解为什么hosts文件不起作用。但它应该解决你遇到的问题。确保将普通DNS服务器设置为转发器。

答案 4 :(得分:1)

服务器是否实际收到请求,您是否正确处理主机名(别名)?

  

添加到我的.hosts文件后

检查您的网络服务器日志,查看请求是如何进入的......

curl具有转储发送的请求和接收响应的选项,它被称为跟踪,它将被保存到文件中。

- 追踪

如果您缺少主机或标头信息 - 可以使用config选项强制执行这些标头。

我会在命令行上运行curl请求,然后尝试在PHP中实现。

配置选项是

-K / - config

与curl相关的选项在这里

- 跟踪           启用对给定输出文件的所有传入和传出数据(包括描述性信息)的完整跟踪转储。使用“ - ”作为文件名将输出发送到stdout。

      This option overrides previous uses of -v/--verbose or --trace-ascii.

      If this option is used several times, the last one will be used.

-K / - 配置           指定从哪个配置文件读取curl参数。配置文件是一个文本文件,其中可以写入命令行参数,然后将其用作实际编写的参数           命令行。选项及其参数必须在同一个配置文件行中指定,用空格,冒号,等号或其任意组合分隔(但是,首选的分隔符 -           tor是等号)。如果参数包含空格,则参数必须用引号括起来。在双引号内,可以使用以下转义序列:\,\“,\ t,\ n,           \ r和\ v。忽略任何其他字母前面的反斜杠。如果配置行的第一列是'#'字符,则该行的其余部分将被视为注释。每个只写一个选项           配置文件中的物理行。

      Specify the filename to -K/--config as '-' to make curl read the file from stdin.

      Note that to be able to specify a URL in the config file, you need to specify it using the --url option, and not by simply writing the URL on its own line. So, it could look similar to this:

      url = "http://curl.haxx.se/docs/"

      Long option names can optionally be given in the config file without the initial double dashes.

      When curl is invoked, it always (unless -q is used) checks for a default config file and uses it if found. The default config file is checked for in the following places in this order:

      1) curl tries to find the "home dir": It first checks for the CURL_HOME and then the HOME environment variables. Failing that, it uses getpwuid() on UNIX-like systems (which  returns  the  home  dir
      given the current user in your system). On Windows, it then checks for the APPDATA variable, or as a last resort the '%USERPROFILE%\Application Data'.

      2)  On windows, if there is no _curlrc file in the home dir, it checks for one in the same dir the curl executable is placed. On UNIX-like systems, it will simply try to load .curlrc from the deter-
      mined home dir.

      # --- Example file ---
      # this is a comment
      url = "curl.haxx.se"
      output = "curlhere.html"
      user-agent = "superagent/1.0"

      # and fetch another URL too
      url = "curl.haxx.se/docs/manpage.html"
      -O
      referer = "http://nowhereatall.com/"
      # --- End of example file ---

      This option can be used multiple times to load multiple config files.

答案 5 :(得分:1)

发出请求
C:\wnmp\curl>curl.exe --trace-ascii -H 'project1.loc' -d "uuid=d99a49d846d5ae570
667a00825373a7b5ae8e8e2" http://project1.loc/Users/getSettings.xml

导致-H日志文件包含:

== Info: Could not resolve host: 'project1.loc'; Host not found
== Info: Closing connection #0
== Info: About to connect() to project1.loc port 80 (#0)
== Info:   Trying 127.0.0.1... == Info: connected
== Info: Connected to project1.loc (127.0.0.1) port 80 (#0)
=> Send header, 230 bytes (0xe6)
0000: POST /Users/getSettings.xml HTTP/1.1
0026: User-Agent: curl/7.19.5 (i586-pc-mingw32msvc) libcurl/7.19.5 Ope
0066: nSSL/1.0.0a zlib/1.2.3
007e: Host: project1.loc
0092: Accept: */*
009f: Content-Length: 45
00b3: Content-Type: application/x-www-form-urlencoded
00e4: 
=> Send data, 45 bytes (0x2d)
0000: uuid=d99a49d846d5ae570667a00825373a7b5ae8e8e2
<= Recv header, 24 bytes (0x18)
0000: HTTP/1.1 403 Forbidden
<= Recv header, 22 bytes (0x16)
0000: Server: nginx/0.7.66
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 11 Aug 2010 15:37:06 GMT
<= Recv header, 25 bytes (0x19)
0000: Content-Type: text/html
<= Recv header, 28 bytes (0x1c)
0000: Transfer-Encoding: chunked
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 25 bytes (0x19)
0000: X-Powered-By: PHP/5.3.2
<= Recv header, 56 bytes (0x38)
0000: Set-Cookie: SESSION=m9j6caghb223uubiddolec2005; path=/
<= Recv header, 57 bytes (0x39)
0000: P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 118 bytes (0x76)
0000: 6b
0004: <html><head><title>HTTP/1.1 403 Forbidden</title></head><body><h
0044: 1>HTTP/1.1 403 Forbidden</h1></body></html>
0071: 0
0074: 
== Info: Connection #0 to host project1.loc left intact
== Info: Closing connection #0

我的主机文件如下:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
...
...
127.0.0.1   project1.loc

答案 6 :(得分:0)

要在尚未通过DNS连接的Apache http服务器上设置虚拟主机,我想使用:

curl -s --connect-to ::host-name: http://project1.loc/post.json

其中主机名是运行网络服务器的计算机的IP地址或DNS名称。 这对于https-Sites也很好。