본문 바로가기
IT/Deploy

nginx, root와 alias 차이와 사용법(예시 포함)

by Cyber_ 2024. 4. 12.

Nginx에서 'root'와 'alias'를 처음 마주했을 때 경로를 지정하는데 꽤 많이 시간을 잡아 먹었다. 그래서 그 차이를 확실히 짚고 가고자 한다.

 

차이

  • root는 설정된 경로에 요청된 URL의 경로를 추가하여 파일을 찾는다. URL의 전체 경로가 파일 시스템의 경로에 직접 매핑 된다.
  • alias는 'location'에 지정된 경로를 제외하고, URL의 나머지 부분을 alias에 지정된 경로에 매핑하는 것이다.

예시

nginx.conf:

server {
    listen 80;
    server_name example.com;

    location /static/ {
        alias /home/app/static_files/;
    }
    
     location /images/ {
        root /home/app/static;
    }
}

간단하게

alias는 "www.example.com/static/" -> "www.example.com/home/app/static_files/", alias로 대체

root는 "www.example.com/images/" ->  "/home/app/static/images/" root를 앞에 추가(!! 도메인은 사라지는 것임)