๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ 42seoul ํ”„๋กœ์ ํŠธ

[42seoul] Webserv : ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ ์›น์„œ๋ฒ„ ๊ฐœ๋ฐœ

by kukim 2021. 10. 28.

๐Ÿค– Webserv

Youpi Webserv by yeha, yeosong and kukim
@42seoul : (2021.02.10 ~ 2021.05.03)

๐Ÿ“– About

  • Nginx์™€ ์œ ์‚ฌํ•œ ์›น์„œ๋ฒ„ ๊ฐœ๋ฐœ
  • ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์Šค, ์“ฐ๋ ˆ๋“œ๊ฐ€ ์•„๋‹Œ ๋ฉ€ํ‹ฐ ํ”Œ๋ ‰์‹ฑ(Multiplexing)์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž‘๋™์€ Nginx์™€ ์œ ์‚ฌํ•˜๊ฒŒ conf ํŒŒ์ผ๋กœ ์›น์„œ๋ฒ„๋ฅผ ์…‹์—…ํ•ฉ๋‹ˆ๋‹ค.
  • Keyword : Socket Programming, HTTP/1.1 Request & Response, HTTP/1.1 Methods RFC, TCP/IP, Network, Multiplexing, Non-Blocking, Asynchronous, CGI, Webserver(Nginx)

๐Ÿ“ Review

  • ํŒ€ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์„ ์œ„ํ•œ ๊ตฌ๊ธ€ ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜, github, Branch Workflow๋ฅผ ์„ค์ •, ๊ธฐ๋Šฅ๋งˆ๋‹ค ์ฝ”๋“œ๋ฆฌ๋ทฐ, ํŽ˜์–ด์ฝ”๋”ฉ, ๋งˆ์ง€๋ง‰์— refactoring ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
  • ์ „๋ฐ˜์ ์œผ๋กœ ์‚ฌ์ „ ์ง€์‹์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— C๋ฅผ ํ™œ์šฉํ•œ ์†Œ์ผ“ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ค‘์‹ฌ์œผ๋กœ ํ•™์Šตํ•ด๊ฐ€๋ฉฐ ์ƒํ™ฉ์— ๋งž๊ฒŒ ๊ฐœ์„ ํ–ˆ๋‹ค.
    • i.e. ์‹ฑ๊ธ€ํ”„๋กœ์„ธ์Šค ์›น์„œ๋ฒ„์˜ ํ•œ๊ณ„ : ๋‹ค์ˆ˜์˜ ํด๋ผ์ด์–ธํŠธ ์‘๋‹ต ์ฒ˜๋ฆฌ ๋”œ๋ ˆ์ด ๋ฌธ์ œ
      • ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์Šค, ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ, ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ ์„ ํƒ์ง€ ์ค‘ ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ ์„ ํƒ
    • i.e. ๋ฉ€ํ‹ฐ ํ”Œ๋ ‰์‹ฑ ์„œ๋ฒ„ ๊ตฌํ˜„ ๋ฌธ์ œ์  : ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์„œ๋ฒ„ ์ž์›์„ read, write ํ•˜๋Š”๋ฐ fd์— Blocking์ด ๊ฑธ๋ ค ์ง€์—ฐ ๋ฐœ์ƒ
      • fcntl()์„ ์‚ฌ์šฉํ•ด ํ•ด๋‹น fd๋ฅผ Non-blocking ์„ค์ •
    • i.e. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋น„์ •์ƒ ์ข…๋ฃŒํ–ˆ์„ ๋•Œ
      • ์„œ๋ฒ„ ์ธก์—์„œ sigpipe์˜ ์‹œ๊ทธ๋„์„ ์ฒดํฌํ•˜๊ณ  ๋น„์ •์ƒ ์ข…๋ฃŒ ๋•Œ ํ•ด๋‹น socket fd ๋‹ซ๊ธฐ
    • i.e. ์„œ๋ฒ„ ๋น„์ •์ƒ ์ข…๋ฃŒ, ์žฌ์‹œ์ž‘ Time-wait bind() ์žฌํ• ๋‹น ๋ฌธ์ œ
      • SO_REUSEADDR ์˜ต์…˜์œผ๋กœ bind() ๋ฌธ์ œ ์ œ๊ฑฐ
    • i.e. RFC ๋ฌธ์„œ์— ๋”ฐ๋ผ HTTP/1.1 ํ—ค๋”๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ํฌ๊ณ  ์ž‘์€ ์–ด๋ ค์›€๋“ค์ด ๋งŽ์•˜๊ณ  ์‹ค์ œ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๊นŠ์–ด์กŒ๋‹ค.
      • e.g. Accept-Language ์š”์ฒญ์— ๋”ฐ๋ผ content negotiation ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด RFC ๋ฌธ์„œ์— ํŠน์ •ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ๊ตฌํ˜„ํ–ˆ๋‹ค.
      • e.g. Transfer-Encoding:chunked ์š”์ฒญ ํ—ค๋”๊ฐ€ ์žˆ๋‹ค๋ฉด ์„œ๋ฒ„์ธก์—์„œ ์ „์†กํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ ์„œ ๋ณด๋‚ด๊ณ  ๋ ๋ฉ”์„ธ์ง€์— ๊ฐœํ–‰ ๋‘๋ฒˆ + EOF
      • e.g. nginx์—์„œ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ์™€ ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋ฅผ ์†์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๊ตฌํ˜„์€ ํด๋ผ์ด์–ธํŠธ ํŠน์ • ๋ฆฌ์†Œ์Šค ์š”์ฒญ์ด redirect ๋˜๋Š” ๋ฆฌ์†Œ์Šค๋ผ๋ฉด ์„œ๋ฒ„ ์ธก์—์„œ ๋‹จ์ˆœํ•˜๊ฒŒ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ redirect ๋˜์—ˆ๋‹ค๊ณ  Location:uri ์‘๋‹ต ํ—ค๋”๋ฅผ ๋ณด๋‚ธ๋‹ค. ์ด์— ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ์žฌ์š”์ฒญ ํ•œ๋‹ค. ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋Š” ์„œ๋ฒ„์ธก์—์„œ Location:uri๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ด์ง€ ์•Š๊ณ  ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์•„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•ด์ค€๋‹ค. ๊ตฌํ˜„ํ•  ๋• ์•„์ฃผ ๋‹จ์ˆœํ•œ ๋ถ€๋ถ„์ด์ง€๋งŒ ๊ธฐ๋Šฅ ์ฐจ์ด๊ฐ€ ํฌ๋‹ค.
  • ๊ฐœ์„  ์‚ฌํ•ญ
    • ํ˜„์žฌ ์„œ๋ฒ„๋Š” ๋ฉ€ํ‹ฐ ํ”Œ๋ ‰์‹ (select() + fcntl()(non-blocking + asynchronous))์œผ๋กœ ๋˜์–ด์žˆ์ง€๋งŒ cgi๋ฅผ ์ œ์™ธํ•˜๊ณ  ์‹ฑ๊ธ€ ํ”„๋กœ์„ธ์Šค๋กœ ์ž‘๋™ํ•œ๋‹ค. nginx์—์„œ๋Š” conf ํŒŒ์ผ์—์„œ Worker Process ์„ ์„ค์ •์œผ๋กœ ํ”„๋กœ์„ธ์Šค ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ˜„์žฌ ๊ตฌํ˜„ํ•œ webserv์—์„œ๋„ cpu ์ž‘์—…์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ์š”์ฒญ์ด ์žˆ๋‹ค๋ฉด ์• ์ดˆ์— ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์Šค,์Šค๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ์ฒ˜๋ฆฌํ•˜๋ฉด ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์™€ refactoring์— ๋นˆ์•ฝํ•˜๋‹ค.
    • ์›น์„œ๋ฒ„์˜ log๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค.

๐Ÿ Run

  • HTTP Method test : GET, HEAD, POST
  • Methods2
  • Content negotiation, Redirect
  • nego_redir
  • Authorization, Server pause 503, Siege test
  • auth_503_siege

๊ตฌํ˜„ ์‚ฌํ•ญ

  • nginx
    • config file
    • Host, Port
    • Server name
    • setup default
    • limit client body size
    • Accepted HTTP Methods for the route
    • autoindex On/Off
    • redirect
    • execute CGI (RFC 3275 ์ฐธ๊ณ )
  • HTTP ํ—ค๋”(RFC 7230 ~ 7235, HTTP/1.1์„ ์ฐธ๊ณ ํ•˜์—ฌ ์•„๋ž˜ ํ—ค๋”๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.)
    • Accept-Charsets
    • Accept-Language
    • Allow
    • Authorization
    • Content-Language
    • Content-Length
    • Content-Location
    • Content-Type
    • Date
    • Host
    • Last-Modified
    • Location
    • Referer
    • Retry-After
    • Server
    • Transfer-Encoding
    • User-Agent
    • WWW-Authenticate

๐Ÿง‘๐Ÿป‍๐Ÿ’ป Author

yeha
kukim
yeosong

ํƒœ๊ทธ

, ,

๋Œ“๊ธ€0