[ 살펴보기 ] Nginx - Basic Configuration
![[ 살펴보기 ] Nginx - Basic Configuration](https://cdn.hashnode.com/res/hashnode/image/upload/v1726928251826/63dd08df-7cd1-4a22-9cad-ce200185ce24.jpeg)
Configuration 파일을 통해 nginx의 기본 동작과 더불어 http request에 대해 어떤 response를 넘겨줄지 혹은 proxy server로서 nginx 뒤에서 작동 중인 application server에 어떻게 request를 넘겨줄지 등을 설정할 수 있다.
UbuntuOS에서 package manager를 통해 nginx를 설치했다면 nginx의 기본 설정파일인 nginx.conf 파일을 /etc/nginx 경로에서 찾아볼 수 있다. 기본 설정파일의 내용은 version에 따라 조금씩 달라질 수 있지만 대략 다음과 같을 것이다.
user www-data;
worker_processes auto;
pid /run/nginx.pid
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connection 1024;
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Nginx는 위의 예제와 같이 key value 형식으로 특정 configuration option에 대한 값을 설정한다. 예를들어 위의 예제에서 우리는 worker_processes라는 옵션에 auto라는 값을 설정해주고 있다.
설정을 위한 각 지시어는 세미콜론 ( ; )으로 끝나야 하고 #으로 시작되는 문자는 comment로 취급된다.
Nginx는 여러 모듈을 통해 동작한다. 그리고 각 모듈에 대한 설정은 모듈의 Directive blocks 안에서 설정할 수 있다. 예를들어 events modules은 events {} block이서 http module에 대한 설정은 http {} block에서 할 수 있다.
아래는 events directive block에서 worker_connections의 값을 수정하는 예시다.
events {
worker_connections 1024;
}
위와 같이 특정 directive block 안에서 설정하는 값이 있는 반면 user나 worker_processes와 같이 특정 directive block이 아닌 configuration root에 위치하는 설정 역시 존재한다. 이러한 설정이 정의되는 block을 main block이라고도 하며 nginx 서버 전체에 적용되는 설정이다.
user www-data;
worker_processes auto;
...
events {
worker_connection 1024;
}
아래와 같이 http block안에 server block을 구성하여 virtual hosting을 위한 설정을 구성할 수도 있다. 아래는 server ip로 request가 오면 /home/ubuntu/test 경로에 있는 index.html 파일을 response 해주는 간단한 예제이다.
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /home/ubuntu/test;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
}
Nginx user ( www-data )가 response로 전달할 파일에 read 권한이 없거나 파일이 위치하는 상위 폴더에 대한 권한이 없으면 해당 파일을 resposne로 전달하지 못하므로 주의하자.
Core Module Directives
이제 Main Block에서 설정할 수 있는 일부 directive를 살펴보자. Main block에서 설정할 수 있는 directive는 nginx의 core module이 제공하는 directive로서 nginx의 기본 동작을 제어하는데 사용된다.
user : nginx가 실행되면 master process가 현재 ubuntu에 접속한 계정의 권한으로 실행된다. 그리고 client request가 발생하면 master process는 worker process를 생성해 client request를 처리하는데 이 때 어떤 user 권한을 통해 worker process를 생성할 것인지 정한다. Default로 www-data user로 설정되어 있으며 www-data user는 nginx를 설치할 때 자동으로 생성된다.
user 유저명; ex) user www-data;worker_process : worker_process의 수를 정한다. 보통 CPU core 숫자당 worker_process 수를 설정한다. 특정 숫자를 설정할 수도 있고 auto로 설정하면 nginx가 현재 system의 core 갯수를 자동으로 detect해서 사용한다.
worker_process 숫자 값 | auto; ex) work_process auto;daemon : nginx를 daemon 모드로 실행할 것인지 결정한다. 만약 off로 설정면 nginx는 background로 실행되지 않고 foreground로 실행된다. default 값은 on이다.
daemon on | off; ex) daemon on;worker_priority : worker process의 priority를 설정한다. default 값은 0이다. -20에서 19까지 설정할 수 있으면 숫자가 낮을 수록 priority가 높다. Kernel process의 priority가 -5이므로 따로 설정해야 하는 경우 kernel process보다 낮을 숫자를 설정하지 않도록 주의하자.
worker_priority 0;
그 외에 main block에서 설정할 수 있는 core module directive는 다양하며 documentation에서 다른 list를 확인할 수 있다. ( context가 main으로 되어 있는 directive가 main block에서 사용할 수 있는 dirctive다 )
Event Module Directives
Nginx는 event module을 통해 request connection을 관리한다. client request가 발생하면 해당 connection을 worker process로 할당하는 작업은 event module을 통해 이루어진다.
Event block에서 설정할 수 있는 directive는 다음과 같다.
accept_mutex : 새로운 connection을 처리할 때 accept mutex 적용 여부를 설정한다. 해당 directive가 on으로 설정되어 있으면 하나의 worker process가 하나의 connection을 처리하고 off로 설정되면 모든 worker process가 새로운 connection에 대한 알람을 받는다.
Nginx 1.11.3 version 이후로 default 값은 off다.
events { accept_mutex off; }worker_connection : 하나의 worker process가 동시에 처리할 수 있는 최대 connection 수를 지정한다. 문제 없이 설정할 수 있는 최대 connection은 실제 server가 운영되는 컴퓨터의 사양에 따라 달라질 수 있다.
events { workder_connection 1024; }use : nginx가 사용할 event model을 설정할 수 있다. Event model은 Nginx가 자동으로 선정하여 사용하므로 해당 directive를 직접 설정할 필요는 없다.
events { use epoll; }
그 외에 events block에서 설정할 수 있는 directive는 다음 documentation에서 확인할 수 있다. ( context가 events라고 되어 있는 directive가 events block에서 사용할 수 있는 directive다 )
![[ 살펴보기 ] RDB - Relationships](https://cdn.hashnode.com/res/hashnode/image/upload/v1739711556668/48dc9e84-a621-42aa-9c9f-5fc5c436f0ec.jpeg)
![[ 살펴보기 ] MySQL - Data types](https://cdn.hashnode.com/res/hashnode/image/upload/v1739593589113/530f8704-4d27-42c9-a451-bb5c63150b99.jpeg)
![[ 살펴보기 ] TypeORM - Transactions, Migration](https://cdn.hashnode.com/res/hashnode/image/upload/v1739106042581/980b8133-61d4-406a-a026-65be9c28eace.jpeg)
![[ 살펴보기 ] TypeORM - Relations](https://cdn.hashnode.com/res/hashnode/image/upload/v1738666874402/b688bd0b-b6bb-4f43-87d8-c1b46b59f1b7.jpeg)
![[ 살펴보기 ] TypeORM - Basics](https://cdn.hashnode.com/res/hashnode/image/upload/v1738666803591/bef5df17-7dc7-4123-ae55-004d5042df39.jpeg)