태섭씨 블로그
HTTP Keep-Alive 본문
종종 찾아보는 건데 잊지 않기 위해 간략히 정리.
[HTTP Keep-Alive]
HTTP 는 기본적으로 Connectionless 구조로, 매 요청시 새로 연결을 생성하는 구조임. 이런 경우 요청이 많아지면 (ex> 여러 이미지를 불러오는 경우 등) 연결 비용이 많이 들어가기 때문에, HTTP Keep-Alive 기능을 사용하면 연결 비용을 절약할 수 있음. Keep-Alive 기능을 이용하면 매번 새로 연결을 생성하지 않고, 기존에 생성한 연결을 이용한다.
[HTTP 1.0]
정식 스펙에는 없고, 클라이언트-서버가 둘 다 지원하는 경우 동작했던 것으로 보임...
(확실치는 않음 ;; http://en.wikipedia.org/wiki/HTTP_persistent_connection 참고)
- Client 에서 header 에 Connection: Keep-Alive 추가하여 보내고
- Server 에서 지원한다면 마찬가지로 Connection: Keep-Alive 를 보내서 해당 기능 지원 (했던 것 같음)
[HTTP 1.1]
별 다른 설정이 없다면 기본 동작이 Keep-Alive (정확히는 Persistent connections) ( http://tools.ietf.org/html/rfc2616#section-8.1.2 )
A significant difference between HTTP/1.1 and earlier versions of HTTP is that persistent connections are the default behavior of any HTTP connection. That is, unless otherwise indicated, the client SHOULD assume that the server will maintain a persistent connection, even after error responses from the server. |
Connection 맺을때에 Keep-Alive를 사용하지 않겠다면 Connection: close 로 명시를 해야함 ( http://tools.ietf.org/html/rfc2616#section-14.10 )
[telnet 테스트 1]
$ telnet www.google.co.kr 80 Trying 74.125.203.94... Connected to www.google.co.kr. Escape character is '^]'. GET /test HTTP/1.1 Host:www.google.co.kr HTTP/1.1 404 Not Found Content-Type: text/html; charset=UTF-8 X-Content-Type-Options: nosniff Date: Fri, 06 Mar 2015 02:18:22 GMT Server: sffe Content-Length: 1429 X-XSS-Protection: 1; mode=block Alternate-Protocol: 80:quic,p=0.08 <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 404 (Not Found)!!1</title> <style> // 생략.... </style> <a href=//www.google.com/><span id=logo aria-label=Google></span></a> <p><b>404.</b> <ins>That’s an error.</ins> <p>The requested URL <code>/test</code> was not found on this server. <ins>That’s all we know.</ins> (연결이 끊기지 않고 유지됨) GET /test2 HTTP/1.1 Connection: close Host:www.google.co.kr HTTP/1.1 404 Not Found Content-Type: text/html; charset=UTF-8 X-Content-Type-Options: nosniff Date: Fri, 06 Mar 2015 02:18:40 GMT Server: sffe Content-Length: 1430 X-XSS-Protection: 1; mode=block Alternate-Protocol: 80:quic,p=0.08 Connection: close <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 404 (Not Found)!!1</title> <style> // 생략 </style> <a href=//www.google.com/><span id=logo aria-label=Google></span></a> <p><b>404.</b> <ins>That’s an error.</ins> <p>The requested URL <code>/test2</code> was not found on this server. <ins>That’s all we know.</ins> Connection closed by foreign host. |
-> 처음 연결시 (GET /test) response 가 온 이후 연결이 유지되었고, (Keep-Alive)
-> 두번째 연결시 (GET /test2) Connection: close 를 명시적으로 헤더에 추가했으므로 response 가 온 이후 연결이 끊김
[telnet 테스트 2]
$ telnet www.naver.com 80 Trying 202.131.30.12... Connected to www.naver.com.nheos.com. Escape character is '^]'. GET /test HTTP/1.1 Host:www.naver.com HTTP/1.1 404 Not Found Server: nginx Date: Fri, 06 Mar 2015 02:22:20 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: close Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache P3P: CP="CAO DSP CURa ADMa TAIa PSAa OUR LAW STP PHY ONL UNI PUR FIN COM NAV INT DEM STA PRE" c04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>네이버 :: 페이지를 찾을 수 없습니다.</title> <link rel="stylesheet" type="text/css" href="/css/err.css?1007" /> <script type=text/javascript> // 생략 </script> </head> <body> <div id="wrap"> <div id="header"> <h1><a href="http://www.naver.com/"><img src="http://static.naver.com/w8/err/lg_naver.gif" alt="NAVER" width="145" height="33" /></a></h1> <p class="menu"><a href="http://www.naver.com/">네이버홈</a> | <a href="http://help.naver.com/">네이버 고객센터</a></p> </div> <div id="container"> <h2>죄송합니다.<br />요청하신 페이지를 찾을 수 없습니다.</h2> // 생략 </address> </div> </div> </body> </html> 0 Connection closed by foreign host. |
-> 처음 연결시 (GET /test) response 가 온 이후 연결이 끊김 (response header Connection: close)
참고 :
http://tools.ietf.org/html/rfc2616 ( HTTP 1.1 Standard (Draft?) )
http://en.wikipedia.org/wiki/Keepalive
http://en.wikipedia.org/wiki/HTTP_persistent_connection
http://web.archive.org/web/20100813132504/http://www.io.com/~maus/HttpKeepAlive.html
http://pungjoo.tistory.com/2
https://beyondj2ee.wordpress.com/2014/01/27/tcp-keep-alive-%ED%80%B5-%EC%A0%95%EB%A6%AC/
'IT' 카테고리의 다른 글
Wildcard SSL 인증서 설치 작업 기록 (0) | 2015.03.12 |
---|---|
URL 에서의 #! (hash-bang) (0) | 2015.02.11 |
Dictionary Map 차이점 (자료구조) (0) | 2015.02.02 |