NGNIX 란?

  • WEB SERVER  PROXY 기능을 동시에 제공해 주는 소프트웨어이다
  • Stream 을사용한 Layer 4 proxying
  • Http 을 사용한 Layer 7 proxying

WEB SERVER 기능

  • 정적 파일을 처리해준다.(HTML, CSS, 이미지 등)

PROXY 기능

  • Load Balance 
  • Backend Route
  • Caching

NGINX 동작 원리

  • Ngnix 는 하나의 master process 와 여러개의 worker process 들로 구성되어있다.
  • Event-based modelOS-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;
  • nameparameter 로 구성되며, 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/logs or /var/run 이다. Process ID 가 1628일 경우 프로세스를 안전하게 종료하고 싶다면:

kill -s QUIT 1628

+ Recent posts