사용자 도구

사이트 도구


nginx:location

nginx location 설정

alias와 root의 차이

alias 는 특정 URL이 서빙할 파일 경로를 변경하는 역할을 한다. root와는 역할이 다르다.

location /images/something/ {
    alias /var/www/something/;
}

이 상태에서 http://example.com/images/something/somepath/myfile.png의 실제 파일상 경로는 /var/www/something/sompath/myfile.png가 된다. 중간에 location에 기술된 /images/something은 경로를 구성할 때 빠진다.

반면에 root /var/www/something 으로 구성했다면 실제 파일상 경로는 /var/www/something/images/something/somepath/myfile.png가 된다.

특정 파일 확장자에 대해 Header 추가하기 등의 작업하기

$uri 변수로 파일 확장자를 확인할 수 있다. $uri는 쿼리 파라미터는 모두 제거한 파일까지의 경로이다.

location / {
  if ($uri ~ ^(.+)\.(eot|ttf|woff)$)
  {
    add_header Access-Control-Allow-Origin *;
  }
}

웹폰트에 대해 Access-Control-Allow-Origin * 헤더를 추가하였다.

URL 중간에 정규표현식 있고, 특정 파일로 무조건 보내기

location ~* ^/some/([0-9a-zA-Z\-_]+)/test {
  rewrite .* /somefile.html;
}

location ~ /somefile.html {
  root /var/www/html;
}

POST redirect

location ~* ^/some/test {
  return 302 /somefile.html;
}

location ~ /somefile.html {
  root /var/www/html;
}

Directory Listing 디렉토리 목록

Enable directory listing

location /somedir {
    autoindex on;
}

internal

internal은 특정 location이 nginx 내부 요청에서만 유효하고 외부 요청에서는 404로 응답하도록 한다. 에러 페이지 등에 사용하면 좋다.

error_page 404 /404.html;
location  /404.html {
  internal;
}

내부 요청은 다음과 같은 것들을 의미한다.

  • error_page로 인해 발생한 요청
  • ngx_http_ssi_module 모듈의 include virtual 명령에 의한 요청
  • ngx_http_rewrite_modulerewrite 명령에 의해 변경된 요청

참조

nginx/location.txt · 마지막으로 수정됨: 2019/06/26 21:24 저자 kwon37xi