반응형

Content-Length

HTTP 1.1에서는 Content-Length라는 헤더로 전달하고자 하는 데이터 사이즈를 표시한다.
웹서버는 다음 과정으로 요청을 처리하고 응답한다.

  • (1) 클라이언트는 웹서버에 데이터를 요청한다.
  • (2) 웹서버는 응답할 데이터 사이즈를 계산한다.
  • (3) 웹서버는 클라이언트에 데이터 사이즈와 함께 데이터를 전달한다.

클라이언트에 데이터가 큰 응답을 전달할 때 (2) 과정에서 오래걸리게 되고 응답 지연을 발생시킬 수 있다.

Transfer-Encoding

Transfer-Encoding 헤더는 비교적 효율적으로 클라이언트에 데이터를 전송하기 위해 사용하는 인코딩 형식을 지정한다.
종류는 아래와 같다.

  • chunked
    • 데이터를 청크 단위로 쪼개어 전송하는 방식
  • compress
    • LZW 알고리즘을 사용하는 방식
  • deflate
    • deflate 알고리즘을 사용하는 방식
  • gzip
    • UNIX gzip 포맷의 전송 방식
  • identity
    • 압축 없는 전송 방식

이 중 청크 인코딩 (상단의 종류 중 chunked에 해당되는) 방식은 위의 (2)와 같은 전체 데이터 사이즈를 계산하는 과정을 거치지 않아 큰 데이터 응답을 할 때 비교적 효율적이다.
응답 헤더에 "Transfer-Encoding: chunked"로 응답하고, Content-Length 헤더는 존재하지 않는다.
데이터를 일정 사이즈(chunk)로 쪼개어 클라이언트에 쪼개진 데이터를 순차적으로 전달한다.
데이터의 마지막 부분을 전송하면 0을 보내어 클라이언트에 데이터를 모두 전송했음을 알리고, 다음 응답을 위해 커넥션을 열린 채로 유지한다.

"Transfer-Encoding: chunked" 예제

./nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    
    gzip  on;              # gzip 활성화
    gzip_comp_level 6;     # 1 ~ 9까지 설정 가능. 숫자가 클 수록 압축률은 올라가지만 속도는 느려짐
    gzip_min_length 256;   # 압축을 적용할 컨텐츠의 최소 사이즈 설정. 지정한 숫자보다 작으면 압축하지 않음.
    gzip_types             # 압축 대상이 될 content-type 설정.
        application/json
        text/html
        text/plain;

    server {
        listen       80;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

nginx 실행

docker run -itd -p 80:80 -v "$(pwd)/nginx.conf:/etc/nginx/nginx.conf" nginx

컨텐츠 요청

$ curl --compressed -v -H "Accept-Encoding: gzip" http://localhost
*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.87.0
> Accept: */*
> Accept-Encoding: gzip
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.23.3
< Date: Mon, 08 May 2023 15:32:45 GMT
< Content-Type: text/html
< Last-Modified: Tue, 13 Dec 2022 15:53:53 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< ETag: W/"6398a011-267"
< Content-Encoding: gzip
<
내용내용

참고

 

반응형

'Development > ETC' 카테고리의 다른 글

[Kakao] 웹으로 카카오 지도 연동하기  (0) 2023.12.24
[Naver] 웹으로 네이버 지도 연동하기  (0) 2023.12.01
[Gradle] Multi-Project  (0) 2021.05.22
[PostgreSQL] 설치  (0) 2021.05.16
[Tool] JMeter  (0) 2021.04.18

+ Recent posts