이 구성을 위해 귀하가 좋아하는 웹 서버를 사용할 수 있습니다. 저는 Nginx를 사용하기 위해 대부분 ST와 함께 작업하기 때문에 결정했습니다.
일반적으로 올바르게 구성된 NGINX는 초당 최대 400K ~ 500K 요청을 처리 할 수 있습니다 (클러스터링). 내가 본 대부분의 것은 초당 50k ~ 80k (클러스터되지 않은) 요청과 30% CPU로드입니다. 물론 이것은 하이퍼 스레딩이 활성화 된 2 x Intel Xeon 이지만 느린 머신에서는 문제없이 작동 할 수 있습니다.
이 구성은 프로덕션이 아닌 테스트 환경에서 사용되므로 서버에 가능한 최상의 기능으로 이러한 기능의 대부분을 구현할 수있는 방법을 찾아야합니다.
먼저 Nginx를 설치해야합니다
yum install nginx
apt install nginx 원래 구성을 백업하면 구성을 재구성 할 수 있습니다. 좋아하는 편집기와 함께 /etc/nginx/nginx.conf 에서 nginx.conf 열어야합니다.
# you must set worker processes based on your CPU cores, nginx does not benefit from setting more than that
worker_processes auto; #some last versions calculate it automatically
# number of file descriptors used for nginx
# the limit for the maximum FDs on the server is usually set by the OS.
# if you don't set FD's then OS settings will be used which is by default 2000
worker_rlimit_nofile 100000 ;
# only log critical errors
error_log /var/log/nginx/error.log crit ;
# provides the configuration file context in which the directives that affect connection processing are specified.
events {
# determines how much clients will be served per worker
# max clients = worker_connections * worker_processes
# max clients is also limited by the number of socket connections available on the system (~64k)
worker_connections 4000 ;
# optimized to serve many clients with each thread, essential for linux -- for testing environment
use epoll ;
# accept as many connections as possible, may flood worker connections if set too low -- for testing environment
multi_accept on ;
}
http {
# cache informations about FDs, frequently accessed files
# can boost performance, but you need to test those values
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s ;
open_file_cache_min_uses 2 ;
open_file_cache_errors on ;
# to boost I/O on HDD we can disable access logs
access_log off ;
# copies data between one FD and other from within the kernel
# faster than read() + write()
sendfile on ;
# send headers in one piece, it is better than sending them one by one
tcp_nopush on ;
# don't buffer data sent, good for small data bursts in real time
# https://brooker.co.za/blog/2024/05/09/nagle.html
# https://news.ycombinator.com/item?id=10608356
#tcp_nodelay on;
# reduce the data that needs to be sent over network -- for testing environment
gzip on ;
# gzip_static on;
gzip_min_length 10240 ;
gzip_comp_level 1 ;
gzip_vary on ;
gzip_disable msie6;
gzip_proxied expired no-cache no-store private auth;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on ;
# request timed out -- default 60
client_body_timeout 10 ;
# if client stop responding, free up memory -- default 60
send_timeout 2 ;
# server will close connection after this time -- default 75
keepalive_timeout 30 ;
# number of requests client can make over keep-alive -- for testing environment
keepalive_requests 100000 ;
}이제 구성을 저장하고 아래 명령을 실행할 수 있습니다.
nginx -s reload
/etc/init.d/nginx start|restart
구성을 먼저 테스트하려면 실행할 수 있습니다.
nginx -t
/etc/init.d/nginx configtest
server_tokens off ;이것은 안전한 DDOS 방어와는 거리가 멀지 만 일부 작은 DDOS를 늦출 수 있습니다. 이 구성은 테스트 환경을위한 것이므로 자체 값을 사용해야합니다.
# limit the number of connections per single IP
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
# limit the number of requests for a given session
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;
# zone which we want to limit by upper values, we want limit whole server
server {
limit_conn conn_limit_per_ip 10 ;
limit_req zone=req_limit_per_ip burst=10 nodelay;
}
# if the request body size is more than the buffer size, then the entire (or partial)
# request body is written into a temporary file
client_body_buffer_size 128k ;
# buffer size for reading client request header -- for testing environment
client_header_buffer_size 3m ;
# maximum number and size of buffers for large headers to read from client request
large_client_header_buffers 4 256k ;
# read timeout for the request body from client -- for testing environment
client_body_timeout 3m ;
# how long to wait for the client to send a request header -- for testing environment
client_header_timeout 3m ;이제 구성을 다시 테스트 할 수 있습니다
nginx -t # /etc/init.d/nginx configtest그런 다음 nginx를 다시로드하거나 다시 시작하십시오
nginx -s reload
/etc/init.d/nginx reload|restart
tsung 으로이 구성을 테스트 할 수 있으며 결과에 만족하면 Ctrl+C 몇 시간 동안 실행할 수 있으므로 CTRL+C를 누를 수 있습니다.
nofile Limit) - LinuxNofile/Max Open Files/File Discriptors/File Handles Handles rhel/Centos 7+에서 제한을 올리는 두 가지 방법이 있습니다. Nginx가 실행되면 마스터 프로세스의 현재 제한 사항을 확인하십시오.
$ cat /proc/$(cat /var/run/nginx.pid)/limits | grep open.files
Max open files 1024 4096 files
ps --ppid $(cat /var/run/nginx.pid) -o %p|sed '1d'|xargs -I{} cat /proc/{}/limits|grep open.files
Max open files 1024 4096 files
Max open files 1024 4096 files
{,/usr/local}/etc/nginx/nginx.conf 에서 worker_rlimit_nofile 지시문을 사용하여 Selinux 정책이 setrlimit 허용하지 않기 때문에 실패합니다. 이것은 /var/log/nginx/error.log 에 표시됩니다
015/07/24 12:46:40 [alert] 12066#0: setrlimit(RLIMIT_NOFILE, 2342) failed (13: Permission denied)
type=AVC msg=audit(1437731200.211:366): avc: denied { setrlimit } for pid=12066 comm="nginx" scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:system_r:httpd_t:s0 tclass=process
nolimit # /etc/security/limits.conf
# /etc/default/nginx (ULIMIT)
$ nano /etc/security/limits.d/nginx.conf
nginx soft nofile 65536
nginx hard nofile 65536
$ sysctl -p
nolimit $ mkdir -p /etc/systemd/system/nginx.service.d
$ nano /etc/systemd/system/nginx.service.d/nginx.conf
[Service]
LimitNOFILE=30000
$ systemctl daemon-reload
$ systemctl restart nginx.service
httpd_setrlimit to true (1) 이로 인해 작업자 프로세스에 대한 FD 제한이 설정됩니다. {,/usr/local}/etc/nginx/nginx.conf 에 worker_rlimit_nofile 지시문을 남겨두고 다음을 루트로 실행하십시오.
setsebool -P httpd_setrlimit 1
기본적으로 max_ranges 제한되지 않습니다. DOS 공격은 많은 범위 반복 (안정성 I/O에 미치는 영향)을 생성 할 수 있습니다.
| 소켓 유형 | 대기 시간 (MS) | 대기 시간 stdev (ms) | CPU로드 |
|---|---|---|---|
| 기본 | 15.65 | 26.59 | 0.3 |
| accept_mutex off | 15.59 | 26.48 | 10 |
| 재사용 | 12.35 | 3.15 | 0.3 |
파일의 다중 스레드 전송은 현재 Linux에서만 지원됩니다. sendfile_max_chunk 한도가 없으면 한 번의 빠른 연결로 작업자 프로세스가 완전히 압수 될 수 있습니다.
map $ssl_preread_protocol $upstream {
"" ssh.example.com:22;
"TLSv1.2" new.example.com:443;
default tls.example.com:443;
}
# ssh and https on the same port
server {
listen 192.168.0.1:443;
proxy_pass $upstream ;
ssl_preread on;
}openssl engine -t )q_disc )가 필요하지 않은 Linux v4.13+.tcp_bbr 활성화되지 않은 경우 : modprobe tcp_bbr && echo ' tcp_bbr ' >> /etc/modules-load.d/bbr.conf
echo ' net.ipv4.tcp_congestion_control=bbr ' >> /etc/sysctl.d/99-bbr.conf
# Recommended for production, but with Linux v4.13rc1+ can be used not only in FQ (`q_disc') in BBR mode.
echo ' net.core.default_qdisc=fq ' >> /etc/sysctl.d/99-bbr.conf
sysctl --system