먼저, Nginx 로 Static 파일을 반환하는 웹서버를 만들어보자. 먼저 웹서버가 하는일을 생각해보자!
포트를 listen 해야한다.
정적파일이 존재하는 곳에서 가져올수 있어야한다.
포트 연결하기
1. http directive 선언
포트를 연결하려면 NGINX 파일을 설정 해주어야한다. 파일이름은 nginx.conf 이고 해당 파일 위치는 /usr/local/etc/nginx (mac 기준)에 위치해 있다. nginx 를 http layer(layer 7) 에서 사용하기 위해서는 http directive 블럭 을 선언해 주어야한다.(directive 가 무엇인지 알고 싶다면 NGINX 개념과 구성 편을 참고)
nginx.conf 파일
http {}
2. server directive 선언
layer 7 을 사용하는 http 블럭을 만들었다면 이제 포트를 듣고 있는 서버를 선언해 주어야한다. 8080 포트를 듣고 있는 server directive를 선언해주자.
이유는 모르겠지만.. ngnix 는 강제로 event directive를 사용하기 때문에, 일단 비어있는 events directive 도 만들어주자
http {
server {
listen 8080;
}
}
evens { }
3. 포트 연결 상태 확인
이제 주소창에 localhost:8080 이라고 입력해준다면 아래와 같은 페이지를 볼 수 있다.
정적 파일 가져오기
"난 이페이지를 만든적이 없는데...?"
이 페이지를 만든적이 없지만 welcome to nginx 라고 적힌 HTML 문서를 볼수 있다. 그 이유는 Nginx 는 기본적으로 root directory 에서 static 파일을 가져오고 있다. 그렇다면 root directory는 어디일까?
default root directory of nginx
/usr/local/Cellar/nginx/1.19.8/html
해당 디렉터리로들어가면 nginx 를 설치할때 함께 설치 되었던 html 파일을 찾을 수 있다.
1. root 로 사용할 디렉터리 만들기
해당 디렉터리를 사용해도 되지만 우리는 우리가 원하는 곳에 root directory을 만들 것이다. 먼저 root 로 사용할 directory 를 만들어주고 index.html 파일을 작성해주자.
cd /Users/Amuse/Documents/dev/server
vim index.html
<html>
<body>
My own content: NGNIX
</body>
</html>
Ngnix 는 하나의 master process 와 여러개의 worker process 들로 구성되어있다.
Event-based model 과 OS-dependent mechanism 을 기반으로 request를 worker processes 들에게 효율적이게 분배한다.
Configuration file 에서 worker process의 개수를 설정할 수 있으며 cpu코어 수와 맞게 적용 할 수도 있다.
Nginx의 기본 설정은 nginx.conf 라는 파일에 정의되어 있고, 위치는 /usr/local/nginx/conf, /etc/nginx, /usr/local/etc/nginx
Master Process
nginx.conf(설정파일) 을 읽고 검증한다.
Worker Process 를 관리한다.
Worker Process
명령을 받아 요청을 처리한다
NGINX 구조와 설정
Nginx는 nginx.conf 에 작성된directive들에 의해서 작동된다. directive의 종류는simple directive와block directive가 있다.
Simple Directive
listen 9000;
name 과 parameter 로 구성되며, space 로 구분되고 끝맺음은 semicolon(;) 으로 정의된다.
Block Directive
example {}
Block Directive는 context를 {} 으로 감싸고 있다
context 밖에 선언된 Directive 들은 모두 main context 라고 한다.
simple directive 들을 감싸고 있다.
함께 사용된 예시
http {
server{
listen 8080;
}
}
Nginx 명령어 | How to start and stop nginx, and reload its configuration
ngnix -s 명령어를 이용해 nginx 를 조작할수 있다.
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files
안전하게 종료 | How to stop nginx
ngnix -s quit
모든 request 를 worker process 들이 처리한 다음에 끝내고 싶다면, ngnix -s quit 명령어를 입력하면 된다. 참고로 ngnix를 시작한 계정이 아니라면 작동하지 않는다..
재시작 | How to reload nginx
ngnix -s reload
configuration 파일이 수정된다면 ngnix -s reload 명령어를 입력하기 까지 적용되지 않는다. master process 는 새로운 configuration 파일에 대한 syntax validity 검사를 수행한다. syntax validity 를 통과한다면 설정이 적용된 worker process 들이 생기고 기존 worker process 들을 종료된다.
만약, syntax validity 를 통과 하지 못한다면 master process 는 수정되기 전 configuration 파일 값을 따르며 계속 작업을 수행시킨다. old process 들은 종료되라는 명령을 받고 수행중인 requests 들을 마무리하고 종료된다.
Process ID 를 이용해서, Kill utility(Unix tool)로 직접 ngnix process 에게 신호를 전달할수 있다. Process ID 는 master process 에 작성되어 있으며 파일명은 nginx.pid 위치는 /usr/local/nginx/logsor/var/run 이다. Process ID 가 1628일 경우 프로세스를 안전하게 종료하고 싶다면: