2010-08-02 74 views
73

使用Apache或Ngnix我總是創建基於真實項目的開發站點,例如http://project1.loc,在添加到我的.hosts文件後,瀏覽器沒有問題使用。設置cURL使用本地虛擬主機

但是,當我嘗試向同一個URL發出cURL請求(http://project1.loc/post.json)時,我從來沒有得到任何東西,只是超時。我假設cURL不關心我的自定義主機,並直接進入一個名稱服務器的信息。

我該如何解決這個問題?

UPDATE 我設置自定義標題「HOST:http://project1.loc」現在我得到400錯誤 - 但他們是瞬時所以我假設捲曲至少使用hosts文件...

回答

91

編輯:雖然這是目前接受的答案,但讀者可能會發現this other answer由用戶John 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頭。)

+1

我得到400與PHP的錯誤,當我手動與curl.exe請求我得到服務器的默認索引,這意味着它不尊重'HOST'標題。 – Xeoncross 2010-08-11 15:29:40

+0

我用虛擬主機在各種服務器上嘗試過它,它可以工作(從命令行)。爲了以防萬一(儘管我認爲它不應區分大小寫),請嘗試使用'Host'而不是'HOST'。正如我所說的,確保你只在Host標頭中使用主機名,沒有別的(後面沒有'http://'和沒有'/ something')。 你是如何設置你的主機文件的? – Bruno 2010-08-11 15:37:15

+0

發佈了更多關於下面這個結果的數據。 – Xeoncross 2010-08-11 15:44:33

1

請使用指向127.0.0.1的真正完全合格的域名(如dev.yourdomain.com)或嘗試編輯正確的主機文件(通常是* nix環境中的/ etc/hosts)。

+0

我在Windows上開發使用'SYSTEM32 /驅動器的/ etc/hosts' – Xeoncross 2010-08-02 18:26:42

+0

您是否使用捲曲或一些原始構建cygwin交叉構建?我這樣說是因爲我不確定每個人如何解決他們的DNS。本機*應該從Windows的主機文件中選取,但是cygwin版本可能需要cygwin版本。無論哪種方式,使用一個指向127.0.0.1的真實域將會起作用,然而事情就會被設置。 – Oli 2010-08-04 21:19:30

+0

我正在使用窗口(以php_fastcgi的身份運行)包含在PHP 5.3中的本機窗口構建。 – Xeoncross 2010-08-05 15:42:19

0

服務器是否實際獲取請求,並且您是否正確處理主機名(別名)?

添加到我的.hosts後檔

檢查你的網絡服務器日誌,看看請求是如何進來的...

捲曲有選擇轉儲請求發送,接收到響應,它被稱爲跟蹤,它將被保存到一個文件。

--trace

如果你缺少主機或標題信息 - 你可以強制這些標題與配置選項。

我會得到在命令行上工作的curl請求,然後嘗試在PHP中實現。

配置選項是

-K/- 配置

是在捲曲相關的選項在這裏

--trace 允許所有傳入和傳出數據的完整跟蹤轉儲,包括描述性信息到給定的輸出文件。使用「 - 」作爲文件名將輸出發送到標準輸出。

 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/- 配置 指定其配置文件中讀出捲曲的參數。配置文件是一個文本文件,可以在其中寫入命令行參數,然後將其用作在實際的 命令行上寫入的命令行參數。選項及其參數必須在相同的配置文件行中指定,用空格,冒號,等號或其任意組合(但優選的分隔符是等號)分隔。如果參數要包含空格,則參數必須用引號括起來。在雙引號內,可以使用以下轉義序列:\,\「,\ 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. 
+0

同樣,我在Windows上使用PHP在運行nginx的相同窗口上在虛擬主機上獲取頁面。無論如何,我向虛擬主機請求了一個虛擬主機 'http:// domain.loc/users/getSettings.xml',這就是access.log顯示爲'127.0.0.1 - - [09/Aug/2010:11: 42:55 -0500]「POST /users/getSettings.xml HTTP/1.1」499 0「 - 」「 - 」'和curl報告'操作超時10000毫秒後收到0字節' 所以我猜cURL是實際上處理虛擬主機,因爲access.log顯示請求。然後再次,它現在可能會使它到正確的域... – Xeoncross 2010-08-09 16:46:42

+0

該行上的「499 0」非常重要。 該進程返回零字節 - 哪個curl正在等待。 並返回一個HTTP 499 - 這是一個奇怪的結果。 調用另一個腳本 - 返回一個靜態字符串以迴應帖子 - 並看到您正在以curl方式獲得響應。 許多人不按照您的期望發佈數據......腳本可能會超時等待響應。 也更改腳本以將輸入記錄到臨時文件,並看到您「正在從您的捲髮請求接收預期的帖子」 – 2010-08-09 18:14:04

+0

添加了您嘗試命令行卷曲 - 以便您可以控制帖子並查看服務器響應? – 2010-08-09 18:15:47

1

看來這不是一個不常見的問題。

首先檢查this

如果沒有幫助,可以在Windows上安裝本地DNS服務器,如this。將Windows配置爲使用本地主機作爲DNS服務器。該服務器可以配置爲對所需的任何虛假域具有權威性,並可將請求轉發給真正的DNS服務器以處理所有其他請求。

我個人認爲這是有點過於頂端,並不能明白爲什麼hosts文件不起作用。但它應該解決你遇到的問題。確保你設置了正常的DNS服務器作爲轉發器。

+0

你能讀一下你自己的答案並重寫嗎?第三行的英文是毫無意義的! – OmarOthman 2017-02-02 19:21:38

+0

收起。嘖嘖,猜我輸入的太快而沒有正確閱讀。 – Matt 2017-02-02 19:31:05

0

發出請求到

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 

我的hosts文件看起來像:

# 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 
+1

'-H'用於完整標題,而不僅僅是主機,所以使用'-H'主機:project1.loc''。另外,儘管存在這個問題,這個請求似乎可以在正確的主機上工作(至少在命令行上通過curl從'hosts'文件中正確獲得)。什麼不起作用(403)似乎是一個認證/授權問題,所以你的服務器似乎阻止了這些請求。我會建議修復這個服務器配置。 – Bruno 2010-08-11 15:52:47

286

其實,捲曲有一個選項明確本:--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

+16

這是殺了我 - 有人可以標記這是正確的答案?這比答案要新,所以沒有選票..但接受的答案是錯誤的(即只適用於某些情況)=( – 2012-08-02 16:50:31

+0

這確實是一個很好的答案,並收到我的投票。只有Xenocross可以標記一個答案已被接受,其他人可能會來這裏,並逐漸投票給你更高 – hobodave 2012-10-19 17:50:55

+10

值得注意的是--resolve僅添加到curl 7.21.3 - 如果你被困在一個較老的主機上(例如Ubuntu 10.04 LTS),那麼-H'主機...'選項仍然是一個有用的回退 – Ken 2012-11-21 10:20:25