HTTP 1.1 스펙에 따르면 반드시 Host 헤더를 추가해야 한다고 한다.


https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html


14.23 Host

The Host request-header field specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource (generally an HTTP URL,

as described in section 3.2.2). The Host field value MUST represent the naming authority of the origin server or gateway given by the original URL. This allows the origin server or gateway to differentiate between internally-ambiguous URLs, such as the root "/" URL of a server for multiple host names on a single IP address.

       Host = "Host" ":" host [ ":" port ] ; Section 3.2.2

A "host" without any trailing port information implies the default port for the service requested (e.g., "80" for an HTTP URL). For example, a request on the origin server for <http://www.w3.org/pub/WWW/> would properly include:

       GET /pub/WWW/ HTTP/1.1
       Host: www.w3.org

A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.


간단히 테스트 해본다.



// $ curl -v -I -o /dev/null  http://.....jpg  아래 커맨드와 동일하다. 알아서 host 헤더를 채운다.

$ curl -v -I -o /dev/null -H "host:plus.google.com" http://.....jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 1.1.1.1

* Connected to ...

> HEAD ....jpg HTTP/1.1

> host:plus.google.com

> User-Agent: curl/7.43.0

> Accept: */*

>

< HTTP/1.1 200 OK

< Server: openresty

< Date: Wed, 01 Feb 2017 08:41:06 GMT

< Content-Type: image/jpg

< Content-Length: 23357

< Connection: keep-alive

< X-Kakao-crc32: 75299291

< Expires: Thu, 31 Dec 2037 23:55:55 GMT

< Cache-Control: max-age=315360000

< Age: 601526

<

  0 23357    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

* Connection #0 to host plus.google.com left intact


하지만 host가 없으면 400 에러가 뜬다.


$ curl -v -I -o /dev/null -H "host:" http://.....jpg

* Connected to ...

> HEAD ....jpg HTTP/1.1

> host:

> User-Agent: curl/7.43.0

> Accept: */*

>

< HTTP/1.1 400 Bad Request





따라서 reverse proxy 설정할 때 종종 빠뜨릴 수 있는데. 


HTTP 1.1 서비스를 위해 proxy_set_header을 추가해야 한다. (이것 땜시 삽질해서 정리해둠)


proxy_set_header Host $host;



Posted by '김용환'
,