카테고리 없음

[Nginx] 명령어 모음

완드로이드 2024. 8. 23. 22:36

심볼릭 링크를 하는 이유

Nginx에서는 파일을 conf.d 디렉토리에서 읽어오는데 conf.d 디렉토리는 /etc/nginx/sites-enabled 에서 파일을 읽어온다

보통 /etc/nginx/sites-enabled 파일을 생성하기 위해서는  /etc/nginx/sites-available에서 파일을 작성 후 심볼릭 링크로 복사한다

 

  • 굳이 왜 이렇게 복잡하게 하는지 궁금해서 찾아봤다
    • 모든 설정을 sites-available 관리하고 필요한 파일만 sites-enabled 활성화할 수 있다
    • 실제 설정파일을 sites-available에 있으므로 실수로 삭제하는 위험이 줄어든다
    •  새 설정 테스트 시 sites-enabled 링크만 생성하고 문제가 있으면 쉽게 제거할 수 있다

 

등의 장점이 있다고 한다

 

 

심볼릭 링크 명령어

sudo ln -s /etc/nginx/sites-available/NodeBoard.conf /etc/nginx/sites-enabled/NodeBoard.conf

 

경로 명령어

cd /etc/nginx/sites-available

 

cd /etc/nginx/sites-enabled

cd /home/ubuntu/NodeBoard

 

파일 생성 명령어

sudo vi NodeBoard.conf

 

삭제 명령어

sudo rm NodeBoard.conf

 

설정확인

sudo nginx -t

 

로그확인

sudo tail -f /var/log/nginx/error.log

 

Nginx 재시작

sudo systemctl restart nginx

 

Nginx 디버그 모드

sudo nginx -t -d

 

 

NodeBoard.conf 파일

server {
    listen 80;


    root /home/ubuntu/NodeBoard/client/build;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api {
        proxy_pass http://localhost:3040;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

 

/api : 백엔드 리버스 프록시 설정 필수

 

listen 80; : 80포트로 들어오는 요청을 처리하겠다는 뜻

 

root /home/ubuntu/NodeBoard/client/build; : 웹사이트의 루트 디렉토리를 지정

 

index index.html; : 디렉토리에 대한 요청이 왔을 때 제공할 파일 지정 여기선 index.html (보동 시작파일)

 

location / {...} : / 경로로 들어오는 요청 처리에 대한 설정파일

 

try_files $uri $uri/ /index.html; : 요청된 파일을 찾는 순서 지정

 1) 먼저 요청된 uri와 정확히 일치하는 파일을 찾는다

 2) 요청된 uri를 디렉토리로 간주하고 그 안에 index.html 파일을 찾는다

 3) 위의 1), 2)가 실패하면 /index.html 파일을 제공한다