실험 1: Nginx 소스 코드 컴파일
1. 소프트웨어 다운로드
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
2. 압축 해제
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz
[root@Nginx ~]# cd nginx-1.28.1/
3. 의존성 설치 및 환경 검사
[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y
[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
--with-http_ssl_module --with-http_v2_module --with-http_realip_module \
--with-http_stub_status_module --with-http_gzip_static_module --with-pcre \
--with-stream --with-stream_ssl_module --with-stream_realip_module
4. 컴파일 및 설치
[root@Nginx nginx-1.28.1]# make
[root@Nginx nginx-1.28.1]# make install
5. Nginx 시작
# nginx 사용자 생성
[root@Nginx ~]# useradd -s /sbin/nologin -M nginx
# 환경 변수 설정
[root@Nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx ~]# source ~/.bash_profile
# 시작
[root@Nginx ~]# nginx
6. systemd 시작 파일 작성
[root@Nginx ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@Nginx ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl enable --now nginx
실험 2: Nginx 원활한 업그레이드 및 롤백
1. 최신 버전 소프트웨어 다운로드
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.4.tar.gz
2. 새 버전 컴파일 (옵션: 버전 정보 숨기기)
[root@Nginx ~]# tar zxf nginx-1.29.4.tar.gz
[root@Nginx ~]# cd nginx-1.29.4/src/core/
[root@Nginx core]# vim nginx.h
버전 정보 수정:
#define NGINX_VERSION "1.29.4"
#define NGINX_VER "MYCOMPANY/" NGINX_VERSION
3. 컴파일
[root@Nginx nginx-1.29.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
--with-http_ssl_module --with-http_v2_module --with-http_realip_module \
--with-http_stub_status_module --with-http_gzip_static_module --with-pcre \
--with-stream --with-stream_ssl_module --with-stream_realip_module
[root@Nginx nginx-1.29.4]# make
4. 원활한 업그레이드
# 이전 버전 백업
[root@Nginx ~]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp -p nginx nginx.old
# 바이너리 파일 교체
[root@Nginx sbin]# \cp -f /root/nginx-1.29.4/objs/nginx /usr/local/nginx/sbin/nginx
# USR2 신호 전송, 새 마스터 프로세스 시작
[root@Nginx sbin]# ps aux | grep nginx
[root@Nginx sbin]# kill -USR2 <기존마스터프로세스PID>
# 이전 버전 워커 프로세스 종료
[root@Nginx sbin]# kill -WINCH <기존마스터프로세스PID>
5. 버전 롤백
# 새 버전 백업
[root@Nginx sbin]# cp nginx nginx.new -p
# 이전 버전 복원
[root@Nginx sbin]# \cp nginx.old nginx -pf
# 이전 버전 다시 로드
[root@Nginx sbin]# kill -HUP <기존마스터프로세스PID>
# 새 버전 프로세스 종료
[root@Nginx sbin]# kill -WINCH <새마스터프로세스PID>
기존 마스터 프로세스는 즉시 종료되지 않지만, 현재 요청을 처리한 후 우아하게 종료(gracefully quit)됩니다. 최종적으로 새 버전의 마스터 및 워커 프로세스만 남게 됩니다.
실험 3: Nginx 설정 파일 관리 및 최적화 매개변수
1. 워커 프로세스 수 설정
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; # 프로세스 수 지정
worker_processes auto; # CPU 코어 수와 자동 매칭
2. CPU 친밀성 바인딩
worker_processes auto;
worker_cpu_affinity 0001 0010 0100 1000; # 4코어 CPU 바인딩
3. Events 모듈 최적화
events {
worker_connections 10000; # 단일 워커 최대 연결 수
use epoll; # epoll 이벤트 모델 사용
accept_mutex on; # 부하 분산 잠금 활성화
multi_accept on; # 한 번에 여러 연결 수신 허용
}
4. 시스템 파일 디스크립터 제한
[root@Nginx ~]# vim /etc/security/limits.conf
* - nofile 100000
* - noproc 100000
root - nofile 100000
[root@Nginx ~]# ulimit -n 100000
[root@Nginx ~]# sudo -u nginx ulimit -n
5. 동시성 테스트
[root@Nginx ~]# dnf install httpd-tools -y
[root@Nginx ~]# ab -n 100000 -c10000 http://172.25.254.100/index.html
실험 4: Nginx에서 PC 사이트 구축
1. 가상 호스트 디렉토리 설정
[root@Nginx ~]# mkdir -p /usr/local/nginx/conf/conf.d
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
include "/usr/local/nginx/conf/conf.d/*.conf";
2. 웹사이트 디렉토리 생성
[root@Nginx ~]# mkdir -p /webdata/nginx/mydomain.com/lee/html
[root@Nginx ~]# echo lee.mydomain.com > /webdata/nginx/mydomain.com/lee/html/index.html
3. 가상 호스트 설정 (root 방식)
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.mydomain.com;
location / {
root /webdata/nginx/mydomain.com/lee/html;
}
location /lee {
root /webdata/nginx/mydomain.com/lee/html;
# 접근 경로: root값 + location값 = /webdata/.../html/lee/
}
}
4. alias (별칭 방식) 설정
server {
listen 80;
server_name lee.mydomain.com;
location /passwd { # 파일 접근
alias /etc/passwd;
}
location /passwd/ { # 디렉토리 접근
alias /mnt/;
}
}
5. 테스트
[root@Nginx ~]# vim /etc/hosts
172.25.254.100 Nginx www.mydomain.com lee.mydomain.com
[root@Nginx ~]# curl www.mydomain.com
[root@Nginx ~]# curl lee.mydomain.com
[root@Nginx ~]# curl lee.mydomain.com/lee/
[root@Nginx ~]# curl lee.mydomain.com/passwd
실험 5: KeepAlived 장기 연결 최적화
1. 장기 연결 시간 초과 설정
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 5; # 연결을 5초 동안 유지
2. 장기 연결 요청 수 설정
keepalive_requests 3; # 단일 연결에서 최대 3개 요청 처리
3. 장기 연결 테스트
[root@Nginx ~]# dnf install telnet -y
[root@Nginx ~]# telnet www.mydomain.com 80
GET / HTTP/1.1
Host: www.mydomain.com
# 여러 요청을 전송하고 Connection 헤더의 변화를 관찰
# 3번째 요청 후, Connection이 close로 변경됨