Apache 또는 Ngnix를 사용하여 항상 실제 프로젝트를 기반으로 개발 사이트를 만듭니다. 파일에 http://project1.loc
추가 한 후 .hosts
브라우저를 사용하는 데 문제가 없습니다.
그러나 http://project1.loc/post.json
동일한 URL에 cURL 요청 ( )을 시도 할 때 시간 초과 외에는 아무것도 얻지 못합니다. 나는 cURL이 내 사용자 정의 호스트를 신경 쓰지 않고 정보를 얻기 위해 이름 서버로 직접 이동한다고 가정합니다.
이 문제를 어떻게 해결할 수 있습니까?
업데이트
사용자 정의 헤더 “HOST : http : //project1.loc “을 설정하고 이제 400 오류가 발생하지만 즉시 발생하므로 cURL이 적어도 hosts 파일을 사용하고 있다고 가정합니다.
답변
실제로 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에서 작동합니다. 로컬 서버에에 대한 인증서가 있다고 가정하면 인증서가 URL 의 호스트 이름 과 일치하지 yada.com
않기 때문에 위의 첫 번째 예는 실패 합니다.yada.com
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
답변
편집 : 이것은 현재 받아 들여지는 답변이지만 독자는 John Hart 가 자신의 요구에 더 잘 적응 한 다른 답변 을 찾을 수 있습니다 . 사용자 Ken 에 따르면 버전 7.21.3에서 도입 된 옵션을 사용합니다 ( 즉,이 초기 답변 이후 2010 년 12 월 에 릴리스 됨 ).
편집 한 질문에서 URL을 호스트 이름으로 사용하고 있지만 호스트 이름 만 있으면됩니다.
시험:
curl -H 'Host: project1.loc' http://127.0.0.1/something
곳이 project1.loc
있습니다 단지 호스트 이름과 127.0.0.1
대상 IP 주소입니다.
(명령 줄이 아닌 라이브러리에서 curl을 사용하는 http://
경우 Host
헤더에 넣지 마십시오 .)
답변
올바른 호스트 파일 (일반적으로 * nix 환경의 / etc / hosts dev.yourdomain.com
)을 가리키는 실제 정규화 된 도메인 이름 (예 :)을 사용 127.0.0.1
하거나 편집 해보십시오.
답변
이것은 드문 문제가 아닌 것 같습니다.
확인 이 처음.
이 방법으로 문제가 해결되지 않으면, 당신은 같은 윈도우, 로컬 DNS 서버를 설치할 수 있습니다 이 . localhost를 DNS 서버로 사용하도록 Windows를 구성하십시오. 이 서버는 필요한 가짜 도메인에 대해 권한을 부여하고 다른 모든 요청에 대해 실제 DNS 서버로 요청을 전달하도록 구성 할 수 있습니다.
나는 개인적으로 이것이 약간 이상하다고 생각하며 호스트 파일이 작동하지 않는 이유를 알 수 없습니다. 그러나 그것은 당신이 가지고있는 문제를 해결해야합니다. 일반 DNS 서버도 전달자로 설정해야합니다.
답변
서버가 실제로 요청을 받고 호스트 이름 (별칭)을 올바르게 처리하고 있습니까?
내 .hosts 파일에 추가 한 후
웹 서버 로그를 확인하여 요청이 어떻게 들어 왔는지 확인하십시오.
curl에는 전송 된 요청과 수신 된 응답을 덤프하는 옵션이 있으며이를 trace라고하며 파일에 저장됩니다.
–자취
호스트 또는 헤더 정보가 누락 된 경우 config 옵션을 사용하여 해당 헤더를 강제 실행할 수 있습니다.
명령 줄에서 작동하는 curl 요청을 얻은 다음 PHP에서 구현하려고합니다.
구성 옵션은
-K /-config
컬과 관련된 옵션은 여기에 있습니다
–trace 설명 정보를 포함하여 들어오고 나가는 모든 데이터의 전체 추적 덤프를 지정된 출력 파일로 활성화합니다. 출력을 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 /-config curl 인수를 읽을 구성 파일을 지정합니다. 구성 파일은 명령 줄 인수를 기록 할 수있는 텍스트 파일로 실제 명령 줄에 기록 된 것처럼 사용됩니다. 옵션과 해당 매개 변수는 공백, 콜론, 등호 또는 이들의 조합으로 구분 된 동일한 구성 파일 행에 지정되어야합니다 (그러나 선호하는 구분자는 등호입니다). 매개 변수에 공백이 포함되는 경우 매개 변수는 따옴표로 묶어야합니다. 큰 따옴표 내에서 다음 이스케이프 시퀀스를 사용할 수 있습니다. \, \ “, \ 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.
답변
에 요청하기
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
답변
아직 DNS를 통해 연결되지 않은 Apache http 서버에 가상 호스트를 설정하려면 다음을 사용합니다.
curl -s --connect-to ::host-name: http://project1.loc/post.json
여기서 host-name은 웹 서버가 실행중인 시스템의 IP 주소 또는 DNS 이름입니다. 이것은 https-Sites에서도 잘 작동합니다.
![](http://daplus.net/wp-content/uploads/2023/04/coupang_part-e1630022808943-2.png)