백엔드/http

HTTP Transfer-Encoding

콤비네이션피자라지 2025. 1. 3. 17:31

HTTP Transfer-Encoding

HTTP 통신시 인코딩 형태를 지정하는 설정 값. 헤더에 설정할 수 있다.
(HTTP 1.1 부터 지원, 설정 시 Content-Length 헤더는 반드시 없어야 함.)
종류 : chunked, compress, deflate, gzip

 

 

 

테스트 

과제 진행 전, API에 응답값을 추가해야 할 일이 생겼었습니다. 
대략적으로 응답 객체가 5 -> 100개로 추가되는 상황이었는데요. 
데이터 크기가 한 번에 크게 증가하다 보니 어디서든 Limit 이 걸리지 않을까 우려되어 테스트를 진행하게 되었고, 

"옵션(Transfer-Encoding) 으로 인해 이슈 없음" 의 결론을 짓게 되었습니다.
테스트 과정을 말씀드리겠습니다.

 

 

nginx 설치

 

nginx, tomcat 서버 구성

1.  reverse-proxy 서버 구조를 구성하기 위해서 nginx 를 설치해줍니다.

$ brew install nginx

$ nginx -V
nginx version: nginx/1.27.3
built by clang 16.0.0 (clang-1600.0.26.4)
built with OpenSSL 3.4.0 22 Oct 2024
TLS SNI support enabled
configure arguments: --prefix=/opt/homebrew/Cellar/nginx/1.27.3 --sbin-path=/opt/homebrew/Cellar/nginx/1.27.3/bin/nginx --with-cc-opt='-I/opt/homebrew/opt/pcre2/include -I/opt/homebrew/opt/openssl@3/include' --with-ld-opt='-L/opt/homebrew/opt/pcre2/lib -L/opt/homebrew/opt/openssl@3/lib' --conf-path=/opt/homebrew/etc/nginx/nginx.conf --pid-path=/opt/homebrew/var/run/nginx.pid --lock-path=/opt/homebrew/var/run/nginx.lock --http-client-body-temp-path=/opt/homebrew/var/run/nginx/client_body_temp --http-proxy-temp-path=/opt/homebrew/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/opt/homebrew/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/homebrew/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/opt/homebrew/var/run/nginx/scgi_temp --http-log-path=/opt/homebrew/var/log/nginx/access.log --error-log-path=/opt/homebrew/var/log/nginx/error.log --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

 

2. 설치가 완료되었으면, nginx.conf 파일을 수정하여 upstream 설정을 추가해줍니다.

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {

    upstream backend {
        server 127.0.0.1:8080;
    }	


    bytes_sent   1;

    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
	    proxy_pass http://backend;	
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

 

3. 설정을 완료 했으면 nginx 재기동  $ nginx -s reload  해줍니다.

 

Spring Boot App

API 서버 역할을 할 MVC 앱을 작성합니다. (기본 port : 8080)

@RestController
public class SimpleApiController {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Response {
        List<String> stringList = new ArrayList<>();
    }

    private void addStringList(List<String> stringList) {
        stringList.add("긴문자열")
	}
    
    @GetMapping(
        value = "/test",
        produces = MediaType.APPLICATION_JSON_VALUE)
    public Response test() {
        Response response = new Response();
        for (int i = 0; i < 1000; i++) {
            addStringList(response.stringList);
        }
        return response;
    }    
}

테스트를 위해 64MB 의 크기를 갖도록 API 를 만들었습니다. 



이제 호출해봅시다. 

 

200 응답을 받았습니다. 데이터의 크기가 64MB가 되는데도 에러가 발생하지 않았습니다. 
응답이 오기까지는 약 5초정도 걸렸는데요. 왜 그런지 응답 값을 분석해보았습니다.

Connection: keep-alive
Transfer-Encoding: chunked

위 설정으로 인하여, 커넥션이 열려있는 상태로 응답을 chunked 단위로 계속 받아오는구나! 추측할 수 있었습니다!허나, 뭔가 찝찝한 기분이 들어서 좀 더 세밀한 분석을 해보기 위하여 WireShark 를 이용해봅시다.

 

WireShark 분석

  • 첫요청 을 시작으로 클라이언트에게 필요한 데이터 인코딩 방법을 응답 헤더로 전달하고있습니다.

  • 그 후 쭉 Chunk를 전달하다

  • 마지막 응답 종료 패킷을 끝으로 통신이 종료됩니다. 

왜 Chunk 사이즈는 16384, 16k 일까요?

nginx 기본 설정 client_body_buffer_size :16k  이어서 입니다.