nginx

SeJin12·2022년 10월 2일
0

설치 명령어

brew install nginx

설치 위치

  1. /usr/local/etc/nginx
  2. /usr/local/nginx/conf
  3. /etc/nginx

서비스 명령어

> nginx # 시작
> nginx -s stop # 중지
> nginx -s reload # 재시작
> nginx -t # conf 파일의 문법을 확인
> nginx -s quit # 현재 실행중인 프로세스가 끝날때까지 기다린 후, 중지

> ps -ef | grep nginx # 서비스 실행 확인

또는

# mac
brew services start|stop nginx
# centos 7 자동 시작 시, 실행 등록
systemctl enable nginx

nginx.conf 기본 구조

server {
	listen 80;
    server_name kim.sejin.com;
    
    location / {
    	root /home/nginx;
        index index.html index.htm; # 초기 페이지 설정
    } # 루트로 접속했을 때, /home/nginx 폴더의 index.html을 서빙
    
    location ~ \.do$ { # 특정 확장자 요청 넘기기 ex. nginx 뒷 단의 WAS
 		proxy_pass http://localhost:8080;
    } # *.do로 끝나는 주소 요청을 8080 포트로 넘길 수 있다
}

server {} 블록은 '하나의 웹사이트 선언' 할 때 사용
location {} 블록은 서버 블록 안에 등장하며, '특정 URL을 처리하는 법'을 정의

일반적으로 conf 파일은 여러 개의 server {} 블록으로 이루어진다
server {} 은 listen & port 7 server_name 으로 구분된다

location directive

location {} 은 URI 매칭을 해주고, 정규식(regex)도 지원한다.

server {
	location / {
    	root /data/www;
    }
    
    location /images/ {
    	root /data;
    }
}

URI가 / 와 매칭된다면, root에 명시된 주소와 URI을 합친다.
만약, 매칭 후보가 여러개라면 '가장 긴 prefix'에 해당하는 주소를 고른다.

/images/ 로 시작하는 URI라면 서버는 /data/images/ 디렉토리로부터 파일을 전송한다. (없을 경우 404)
예를 들어, http://localhost/images/example.png/data/images/example.png 로 바뀐다.

/images/ 로 시작하지 않는 것들은 모두 /data/www로 매핑
예를 들어, http://localhost/some/example.png/data/www/some/example.png로 바뀐다.

로그 설정

nginx.conf

http {
	log_format main '$remote_addr - $remote_use ... ';
    access_log logs/access.log main;
}

log_foramt format 변수명 formatter
access_log 파일 경로 format 변수
나의 Mac 환경에서는 logs/access.log 경로는 usr/local/Cellar/nginx/1.23.1/logs/access.log 경로에 파일이 생성되었다.
access_log /Users/user_name/Documents/logs/access.log main 절대경로로 사용해도 된다.

nginx: [warn] 1024 worker_connections exceed open file resource limit: 256

sh-3.2# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 5568
virtual memory          (kbytes, -v) unlimited

ulimit 은 프로세스의 자원 한도를 설정하는 명령

📘 nginx.conf 파일 내용에 추가하기

worker_rlimit_nofile 1024;

nginx -t 명령어를 통해 conf 파일 문법이 맞는지 확인할 수 있다. (syntax is ok)

TODO

conf 파일 변수 사용하기
로그 파일 설정들




Ref Site

nginx conf
nginx 명령어

0개의 댓글