배경
dreamhack의 Youth-case라는 문제를 푸는 과정에서 다음과 같은 코드를 보게 되었다.
events {
worker_connections 1024;
}
http {
server {
listen 80;
listen [::]:80;
server_name _;
location = /shop {
deny all;
}
location = /shop/ {
deny all;
}
location / {
proxy_pass http://app:3000/;
}
}
}
위 코드에서 보면 events{} 라는 블록을 볼수 있는데 해당 블록이 어떤 행위를 하며 어떤 역할인지에 대해서 정리해 보았다.
Nginx 설정
- ubuntu : /etc/nginx/nginx.conf
- nginx의 모든 설정 파일은 directive에 의해 제어 됩니다. 또한 이런 directive는 simple directive 와 block directive로 2가지 종류를 통해서 선언할 수 있습니다.
simple directive
명령 이름 : 값;의 형태로 끝나느 것을 simple directive라고 합니다.
worker_process 1;
block directive
{}(블록)을 통해서 한번 감싼 형태를 block directive라고 합니다.
http {
server {
location /{
root /path/to/html;
}
}
}
block directive는 안에 또 다른 block directive로 감쌀수 있습니다.
Directive 키워드 예제
http 블록
http 통신과 관련된 모듈의 지시어 블록을 정의하는 블록이다.
server 블록
특정 호스트를 설정하는 블록입니다. http 블록 안에서만 사용할 수 있으며, server 블록에는 하나 이상의 location 블록을 선언할 수 있습니다.
http {
server { # <-
location /{
root /path/to/html
}
}
}location 블록
특정 url 처리하는 방법을 정의하는 블록이다. server 블록 안에 정의가 가능하다.
http {
server {
location /{ # <-
root /path/to/html
}
}
}events 블록
event 블록은 nginx에서 네트워크 환경 설정 블록이다.
- 옵션
- accept_mutex(on | off) : 소켓 통신을 사용할 경우 mutex의 사용/해제를 설정
- accept_mutex_delay(~ms) : 자원 획득시 다시 시도하기 전에 worker process가 기다려야 하는 시간 정의
- worker_connections(int) : 동시에 처리 가능한 접속자 수 정의.
events{
accept_mutex on;
accept_mutex_delay 500ms;
worker_connections 1024;
}Reverse Proxy 설정
nginx에서는 prox_pass 라는 것을 통해서 rever proxy를 설정할 수 있습니다. 이를 통해서 내부 서버의 외부 노출을 방지하고, SSL 사용등을 통한 보안과 요청을 분산시키는 로드밸런싱의 역할을 수행할 수 있습니다.
http {
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8001;
}
}
}