2018년 3월 27일 화요일

[NGINX] 2. Redirect 모듈

1. 재작성 / 변수 / 쿠키 / 브라우저 에이전트 / 전송률 제한
  - 영구 리다이렉션 301 규칙 생성하여 새로운 URL을 가리키는 경우
  예) rewrite ^/blog/test-post.html$ /blog/test-post/ pernant;
  - 일시적인 리다이렉션 302 규칙 생성하여 새로운 URL을 가리키는 경우
  예) rewirte ^/blog/test-post.html$ /blog/test-post/ ;
  - 조건부 변수 재작성
  예) If ($arg_special ~* (beta|alpha|gamma) {rewrite ^(.*)$ http://www.example1.com/greek/$1/;}
  => special 이라는 GET 매개변수가 beta, alpha, gamma 포함된 URL을 입력하면 특정 URL로 리다이렉션 함
  - 조건부 쿠키 재작성
  예) if ($cookie_env ~* "testing") {rewrite ^(.*)$ /testing/$1;}
  => 쿠키에 따라 uri 리다이렉션
  - 브라우저 에이전트
  예) if ($http_user_agent ~* '(iphone|ipod)') {set $iphone_request '1';}
        if ($iphone_request = '1') { rewrite ^.+ http://m.example.com/$uri;}
  - 전송률 제한
  예) if ($http_user_agent ~ "MSIE") { limit_rate 5k; }
  => 사용자 에이전트가 MSIE일 경우 전송률을 초당 5킬로바이트로 제한함

2. HTTP referer와 요청 제한
  - 봇을 체크할 수 있는 $http_referer를 체크하여 403 http code 리턴
  예) if ($http_referer ~* (babes|click|dimond...)) { return 403 }

3. 서비스 운용 중 유지보수 페이지 서비스
  - 서비스 정비 중 HTML 파일 생성 -> 임시 환경설정 파일 생성 -> 배포를 위한 스크립트 작성 후 실행 -> 업데이트가 완료 후 원래 환경설정 파일로 복구 후 리로드

4. Nginx을 이용한 워드프레스 구축 예제
  - 워드프레스 다운로드 및 압축 해제
  - Nginx.conf 파일의 http 지시어에 다음 환경설정을 추가함
    ~ location ~* \. (jpg|jpeg|gif|css|png|js|ico|html)$ {expires max;}
  - 서버 재시작
    ~ kill -HUP <master PID>
  - 블로그 설치

5. Nginx을 이용한 드루팔 구축 예제
  - 드루팔 다운로드 및 압축 해제
  - Nginx.conf 파일 수정
    ~ type_files $uri $uri\ /index.php?q=$uri;
  - 서버 재로드
    ~ kill -HUP <master PID>
  - 드루팔 설치

6. 아파치 .htaccess를 엔진엑스 재작성 변환 예제
  - RewriteCond => if / eles 문
  - RewriteRule => rewrite

7. 맵모듈 을 이용한 환결설정(switch ~ case 문 개념)

[예제 1]
map $http_host $name {
  hostname;
  default 0;
  example.com 1;
  *.example.com 1;
  test.com 2;
  *.test.com 2;
}
if ($name ~* 1) {
  <재작성 규칙들>
}

[예제 2]
map $cookie_env $type {
  default /production/;
  testing /testing/;
  staging /staging/;
  production / production/;
}
rewirte ^(.*)$ $type/$1;