nginx

  • 트래픽이 많은 웹사이트를 위한 확장성을 가진 비동기 이벤트 기반 구조의 웹 서버 소프트웨어 (event driven)
    • 비동기 event-driven 구조 : 서버로 들어오는 여러 커넥션을 event handler에서 비동기 방식으로 처리해서 먼저 처리되는 것 부터 로직이 진행된다.
    • 적은 메모리로 서버를 운영할 수 있다.
  • 가벼움, 높은 성능 => 가장 많이 사용되는 웹 서버
  • 다수의 연결을 효과적으로 처리

 

 

nginx 기능

  • HTTP Server
    • 정적 파일을 처리하는 web server
  • Reverse Proxy Server
    • 클라이언트 요청을 받아 웹서버로 전달하여 응답을 받은 뒤 다시 클라이언트에 전달하는 서버
    • 클라이언트 - 웹 서버 사이에 존재하는 서버
    • 클라이언트 요청을 application server에 배분한다.
      • 클라이언트 80 포트 요청을 8080, 8081 등 여러 application server로 보내준다.
    • 각 application server에 요청을 배분하여 부하를 분산해서 로드밸런싱 한다.
      • 로드밸런싱되면 포트, ip 정보를 외부에서 알 수 없게 되어 보안적인 이점을 가진다.

 

 

nginx 설치

sudo apt-get update
sudo apt-get install -y nginx

설치

 

 

 

sudo vi /etc/nginx/sites-available/django_test
server {
        listen 80;
        server_name {서버IP주소};

        location /static/ {
                root /home/ubuntu/{프로젝트 폴더}/staticfiles/;
        }

        location / {
                include proxy_params;
                proxy_pass http://127.0.0.1:8000;
        }
}

sites-available에 server 설정 작성

 

 

 

sudo ln -s /etc/nginx/sites-available/django_test /etc/nginx/sites-enabled

sites-enables에 연결시킨다.

 

 

 

sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill

80 포트 프로세서 종료

 

 

 

sudo systemctl restart nginx
systemctl status nginx.service

restart, status 확인

 

 

 

 

nginx 리눅스 명령어

  • 실행, 중지
기동 $ sudo systemctl start nginx
종료 $ sudo systemctl stop nginx
재시작 $ sudo systemctl restart nginx
재로드 $ sudo systemctl reload nginx
서비스 상태 확인 $ sudo systemctl status nginx
systemctl restart nginx # nginx 재시작
systemctl start nginx # nginx 시작
systemctl stop nginx # nginx 중지

 

 

 

 

nginx 구조

nginx.conf

/etc/nginx/nginx.conf

user  nginx;
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;

    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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf
}

에러 로그 저장 경로

error_log /var/log/nginx/error.log

 

프로세스 아이디 (pid) 저장

pid /var/run/nginx.pid

 

worker_connections

동시에 처리할 수 있는 접속자 수 (기본 1024)

include파일에 작성된 내용을 현재 파일로 가져온다.

포함 시킬 외부파일 정의

 

default_type

웹서버의 기본 content-type 정의

 

log_format

로그 형식 지정, 지정한 형태에 따라 로그 기록 된다.

access_logaccess_log /var/log/nginx/access.log

접속 로그 쌓이는 경로

 

sendfiles

endfile() : 한 파일의 디스크립터와 다른 파일의 디스크립터 간에 데이터 복사

sendfile()함수 사용 여부 지정한다.

 

keepalive_timeout

클라이언트에서 연결이 유지될 시간 정의 (기본 65)

 

default.conf

여러개의 파일을 만들어서 서버 관련 설정을 만든다.

 

 

 

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

listen

해당 포트로 들어오는 요청을 server{} 블록 내용에 맞게 처리한다.

 

server_name

로컬에서 작업하고 있는 내용을 nginx로 띄우려면 localhost라고 작성한다.

호스트 이름 지정

error_pagelocation 으로 url이 /50x.html과 일치하면 해당 경로에 존재하는 /50x.html 파일을 보여준다.

요청 결과의 http 상태코드가 지정된 http 상태코드가 일치할 경우 해당 url로 이동한다.

 

location /

처음 요청이 들어왔을 때 보여줄 페이지가 속한 경로와 초기 페이지 index 지정한다.

 

 

 

 

'Back-end' 카테고리의 다른 글

django + vue 배포  (0) 2023.03.01