Nginx 기본config 설정
nginx의 기본적인 활용에 앞서서 conf에 사용되는 옵션들을 정리해보자.
주로 사용되는 옵션은 정해져있지만, 이 옵션들이 어떤 역할을 하는지에 대해 알아보고 상황에 맞게 커스텀 하자.
user nginx; # 프로세스의 실행되는 권한. 보안상 root를 사용하지 않습니다.
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream docker-server {
server server:8080;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /api {
proxy_pass http://docker-server;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /socket {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_pass http://docker-server;
}
}
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;
server_tokens off;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
상단
- worker_processes
워커 프로세스를 몇개 생성할 것인지 지시한다.
1이면 모든 요청을 하나의 프로세스로 실행한다. 보통 auto로 놓는다.
- error_log
로그 레벨을 설정한다. debug | info | notice | warn | error | crit 같은 종류가 있다.
- pid
마스터 프로세스id 정보가 저장된다.
event 블록
- worder_connections
하나의 프로세스가 처리할 수 있는 커넥션의 숫자.
http 블록
- include
옵션 항목을 설정해둔 파일의 경로를 지정한다.
보통 파일 확장자명과 MIME타입 목록을 지정한다.
- defulat_type
옥텟 스트림 기반의 http를 사용한다.
- upstream 블록
origin서버라고도 하는데, WAS를 의미한다.
(nginx는 downStream에 해당한다.)
nginx와 연결한 WAS를 지정하는데 사용한다.
하위에 있는 server 지시어는 연결할 ㅇWAS의 host주소:포트를 지정한다.
- server블록
하나의 웹사이트를 선언하는데 사요된다.
server 블록이 여러개면 한개의 호스트에 여러 웹사이트를 서빙가능하다.
- listen
이 웹사이트가 바라보는 포트
- server_name
클라이언트가 접혹하는 서버(도메인). 실제로 들어온 request의 header에 명시된 값이 일치하는지 확인하여
server를 분기한다.
- location 블록
server 블록 안에서 특정 웹사이트의 url을 처리하는데 사용한다.
예를 들어 https://web.com/internal과 https://web.com/external 로 접근하는 요청을 다르게 처리하고 싶을 때 사용한다.
부의 root는 웹사이트가 바라보는 root 폴더의 경로를 의미한다.