cf-1. 바보개발님 블로그 먼저보기
https://minholee93.tistory.com/entry/Nginx-Buffer-Timeout?category=926406
[Nginx] Buffer & Timeout
이전글에 이어서 Nginx의 configuartion에 대해 알아보도록 하겠습니다. 11. Buffer & Timeout 이번글에서는 buffer와 timeout을 적절히 사용해 ngimx의 performance를 증가시키는 방법에 대해 알아보도록 하겠습..
minholee93.tistory.com
1. 실습 후 완성된 nignx.conf 파일
user www-data;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
# Buffer size for POST submissions
client_body_buffer_size 10K;
client_max_body_size 8m;
# Buffer size for Headers
client_header_buffer_size 1k;
# Max time to receive client headers/body
client_body_timeout 12;
client_header_timeout 12;
# Max time to keep a connection open for
keepalive_timeout 15;
# Max time for the client accept/receive a response
send_timeout 10;
# Skip buffering for static files
sendfile on;
# Optimise sendfile packets
tcp_nopush on;
server {
listen 80;
server_name http://13.124.129.118/;
root /sites/demo;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~\.php$ {
# Pass php requests to the php-fpm service (fastcgi)
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
}
}
}
buffer와 timeout은 모두 'optimizing processes'를 위한 것이다. 그리고 다른 configure와 다르게 다소 난이도가 있기때문에 완전히 이해한 것이 아니라면 default값으로 놔두는 것이 좋다.
2. buffering?
(1) 개념
다음 목적지에 데이터를 보내기 전에 memory(RAM(buffer))에다 데이터를 wirte해두는 것. reading 와 writing 사이에 layer of protection 을 두는 개념이다.
예시1 - request 받는 과정
1. nginx가 TCP port 80에서 request를 받음
2. request의 데이터를 RAM에 write함 (buffering)
3. 그러다가 데이터가 RAM의 용량을 초과하면 disk에 wirte함
예시2 - response 돌려주는 과정
1. nginx가 disk에서 static file을 받음
2. static file을 RAM에 write함 (buffering)
3. RAM에 write한 static file로 response 만들어서 client 에 보냄
(2) directives
client_body_buffer_size: client로부터 post 데이터를 받을 때 buffering에 사용할 memory를 지정함. 10K이면 10KB.
- 필요이상으로 지정해버리면 server의 momory를 낭비하게 된다.
- 너무작게 지정해버리면 데이터의 일부를 disk에 write해야하기 때문에 느려진다.
client_max_body_size: client로부터 post 데이터의 최대 용량을 지정함. 8m이면 8MB.
client_header_buffer_size: request header에 할당할 buffring memory.
(cf) nginx의 data-size unit
http://nginx.org/en/docs/syntax.html
Configuration file measurement units
Configuration file measurement units Sizes can be specified in bytes, kilobytes (suffixes k and K) or megabytes (suffixes m and M), for example, “1024”, “8k”, “1m”. Offsets may be also specified in gigabytes using g or G suffixes. Time interval
nginx.org
3. timeouts?
(1) 개념
주어진 event의 제한시간을 두는 것. 무한 데이터전송으로 인한 서버파괴를 방지하기 위함.
(2) directives
client_body_timeout: request body의 제한시간. 모든 데이터를 transmit 하는데 걸리는 시간을 말하는 것이 아니라, consecutive read operations에 걸리는 시간을 지칭함. 12 이면 12 milliseconds. 디폴트는 60임.
client_header_timeout: request header의 제한시간.
keepalive_timeout: 더 많은 데이터가 있을 것을 대비하여 engineer가 client에 connection을 open 한채로 있어야 하는 시간. client가 다수의 파일을 전송할 때 유용하다. 전송 사이 사이에 이렇게 connection을 open 해두면 새 connection을 여는데 드는 시간을 아낄 수 있기 때문에 효율성이 올라간다. 그러나 너무 오랜 기간 열어두면 worker_connections 최대치에 도달하는 문제를 일으킬 수 있으므로 적당한 시간을 지정해야한다.
send_timeout: client가 정해진 시간에 response data를 받지 못하면(response data 전부를 미처 받지 못했음을 뜻하는 것이 아니라, 아주 일부분이라도 받지 못했음을 뜻함) 모든 response를 중단시킴.
4. 그 외 performance를 높이는데 큰 기여를 하는 특별한 directives
sendfile: disk에서 staticfile를 보낼 때 buffering 과정을 생략한다. disk에서 client로 바로 보냄.
tcp_nopush: data packets size를 최적화해준다
'Tool > Nginx' 카테고리의 다른 글
| [NGINX] 4-1. Performance - Headers & Expires (0) | 2021.12.18 |
|---|---|
| [NGINX] 3-11. Configuration - Adding Dynamic Modules (0) | 2021.12.18 |
| [NGINX] 3-9. Configuration - Worker Processes (0) | 2021.12.18 |
| [NGINX] 3-8. Configuration PHP Processing (0) | 2021.12.18 |
| [NGINX] 3-7. Configuration - Inheritance & Directive types (0) | 2021.12.18 |
댓글