시작하기
기본 설정
Nginx 설정은 계층적 블록 구조를 씁니다. worker_processes가 프로세스 수를 정합니다. http 블록이 server 블록을 담고, server 블록이 location 블록을 담습니다. 리로드 전에는 항상 nginx -t로 테스트하세요.
# /etc/nginx/nginx.conf
worker_processes auto;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
# Test config: nginx -t
# Reload: nginx -s reload
# Start: systemctl start nginx디렉티브와 컨텍스트
컨텍스트는 중첩됩니다. main > events/http/mail > server > location. 단순 디렉티브는 세미콜론으로 끝나고 블록 디렉티브는 중괄호를 씁니다. 일부 디렉티브는 특정 컨텍스트에서만 유효합니다.
# Contexts are hierarchical blocks
# Global context (main) - outside any block
user nginx;
worker_processes auto;
# events context
events {
worker_connections 1024;
}
# http context
http {
# server context
server {
listen 80;
# location context
location / {
root /var/www/html;
}
}
}
# Directives: simple (name value;) or block (name { ... })테스트와 리로드
nginx -t는 문법을 검사하고 참조된 파일(인증서, 로그)을 열려고 합니다. 리로드 전에 항상 실행하세요. reload는 SIGHUP을 보내며, 워커는 현재 요청을 마친 뒤 재적재합니다. restart는 잠깐의 다운타임을 일으킵니다.
# test configuration for syntax errors
nginx -t
# test and print full config
nginx -T
# reload config without dropping connections
nginx -s reload
# stop gracefully (finishes current requests)
nginx -s quit
# stop immediately
nginx -s stop
# reopen log files (after log rotation)
nginx -s reopen
# systemd alternative
systemctl reload nginx
systemctl restart nginxMain vs HTTP 컨텍스트
worker_processes와 events는 main 컨텍스트에 있습니다. 라우팅, 프록시, 캐싱, server 블록은 http에 있습니다. SSL, 로깅, gzip은 http에서 설정해 server/location이 상속할 수 있습니다.
# main context - global settings
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
events {
worker_connections 4096;
}
http {
# all HTTP-related config lives here
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}Include 파일
include가 설정을 모듈화합니다. conf.d 글롭(*.conf)이 현대적 관례이고, 심링크를 쓰는 sites-enabled는 Debian/Ubuntu 패턴입니다. mime.types가 파일 확장자를 Content-Type으로 매핑합니다.
# main config: /etc/nginx/nginx.conf
http {
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name example.com;
root /var/www/html;
}
# Disable a site: remove the file from sites-enabled/
# Available sites kept in sites-available/, symlinked into enabled/명령줄 연산
nginx -V는 내장 모듈을 포함한 컴파일 옵션을 보여 줍니다. -g는 설정을 무시하는 디렉티브를 정하고, -c는 기본 설정을 건드리지 않고 다른 설정을 테스트할 때 유용합니다.