# 예시 nginx.conf 파일
# worker 프로세스 수 설정
worker_processes auto;
# 이벤트 모듈 설정
events {
worker_connections 1024;
}
# HTTP 모듈 설정
http {
# MIME 타입 설정
include mime.types;
default_type application/octet-stream;
# 로그 포맷 설정
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 업스트림 서버 설정
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
# 서버 설정
server {
# 리스닝 주소와 포트 설정
listen 80;
server_name localhost;
# 루트 디렉토리 설정
root /var/www/html;
# 로그 설정
access_log logs/access.log main;
# 로케이션 설정
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
1. **worker_processes**: worker 프로세스 수를 설정합니다. `auto`로 설정되어 있으면 시스템의 CPU 코어 수에 맞춰 자동으로 설정됩니다. worker 프로세스는 동시에 요청을 처리하는 주체입니다.
2. **events**: 이벤트 모듈 설정을 정의합니다. `worker_connections`는 한 worker 프로세스가 동시에 처리할 수 있는 연결 수를 나타냅니다.
3. **http**: HTTP 모듈 설정을 정의합니다. `include mime.types`는 MIME 타입 설정 파일을 포함하고, `default_type`은 정의되지 않은 파일의 기본 MIME 타입을 설정합니다.
4. **log_format**: 로그 포맷을 설정합니다. `main`은 주요 로그 포맷을 정의하며, `$remote_addr`, `$remote_user`, `$time_local`, `$request` 등의 변수를 사용하여 로그 메시지를 구성합니다.
5. **upstream**: 업스트림 서버 설정을 정의합니다. `backend`라는 업스트림 이름으로 `backend1.example.com`과 `backend2.example.com`의 서버를 정의합니다. 업스트림 서버는 프록시 백엔드 서버와의 연결을 관리하며, 부하 분산 및 장애 조치에 사용됩니다.
6. **server**: 서버 설정을 정의합니다. `listen`은 서
버가 수신 대기할 주소와 포트를 설정하며, `server_name`은 서버에 대한 도메인 이름을 설정합니다. `root`는 요청이 들어왔을 때 정적 파일의 기본 디렉토리를 설정합니다.
7. **access_log**: 액세스 로그를 설정합니다. `logs/access.log` 파일에 `main` 포맷으로 로그를 기록합니다.
8. **location**: 로케이션 설정을 정의합니다. `/` 경로에 대한 설정으로, `proxy_pass`를 통해 요청을 프록시 백엔드 서버(`backend`)로 전달합니다. `proxy_set_header`를 사용하여 요청 헤더를 수정하거나 추가할 수 있습니다. 예를 들어, `X-Real-IP` 헤더를 클라이언트의 IP 주소로 설정하거나 `X-Forwarded-For` 헤더를 프록시 서버로 전달된 클라이언트의 IP 주소로 설정할 수 있습니다.
이것은 예시적인 nginx.conf 파일이며, 실제 설정은 서버의 요구에 따라 달라질 수 있습니다. Nginx의 설정은 유연하며, 이를 통해 웹 서버를 효과적으로 운영할 수 있습니다.