user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
.conf파일만 있으면 자동 적용된다.conf.d에 여러 default.conf파일을 두고 nginx.conf에서 이를 include하여 서버 관련 설정을 추가할수 있다.include /etc/nginx/conf.d/*.conf;
해당 설정 파일에서는 server블록을 정의해서 여러 도메인/포트/경로에 따라 각각 다른 설정이 가능하다.
이를 도식화하면 다음과 같다.

nginx.conf에서 include /etc/nginx/conf.d/*.conf;로 설정하여 conf.d/ 디렉토리 내부에 있는 모든 .conf파일들을 적용할 수 있다.
이 각각의 conf.d/*.conf파일들은 server블록을 설정하여 여러 개의 포트포워딩을 설정할 수 있다.
nginx.conf에 include /etc/nginx/sites-enabled/*;가 있어야 적용된다.sites-available에 들어있는 설정 파일을 심볼릭 링크로 연결해서 어떤 설정이 활성화 되어있는지 확인하고 제어하는 역할을 가진다.server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8000;
}
}

server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8000;
}
location /error {
root /usr/share/nginx/html;
index error.html;
}
}

user nginx; # nginx 프로세스를 실행할 리눅스 사용자 계정
worker_processes auto; # 동시 처리할 worker프로세스 개수 (auto : 코어 수에 맞춰 설정)
error_log /var/log/nginx/error.log # 에러 로그 파일 경로 지정
pid /run/nginx.pid; # 마스터 프로세스의 PID를 기록할 파일 경로
events {
worker_connections 1024; # 각 worker가 동시에 처리할 수 있는 최대 연결 수
multi_accept on; # 여러 연결을 수락할지 여부 (on | off)
use epoll; # 사용할 이벤트 처리 모델 지정
}
listen : 해당 블록이 바이딩할 포트 지정server_name : 해당 블록이 적용될 도메인 이름 (중복 가능)root : 정적 파일의 기준 경로index : 디렉토리 접근 시 기본으로 보여줄 파일 이름proxy_pass : 요청을 다른 서버로 전달error_page : 오류 발생 시 보여줄 페이지 경로try_files : 요청 경로에 다른 파일 확인 및 대체 응답 설정return : 특정 응답 상태 및 url로 리다이렉션rewrite : 요청 url을 다른 경로로 재작성upstream my_backend {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://my_backend;
}
}