본문 바로가기
linux

nginx rewrite 설정

by 후린트 2020. 6. 12.
반응형

nginx에서 uri에서 특정 값을 포함하거나 파라미터에서 특정 값이 포함됐을때 
다른 uri로 rewrite시키는 설정이다.

nginx variable에 대한 설명은 아래 사이트에서 확인 가능하다.

nginx.org/en/docs/http/ngx_http_core_module.html

 

Module ngx_http_core_module

Module ngx_http_core_module Directives Syntax: absolute_redirect on | off; Default: absolute_redirect on; Context: http, server, location This directive appeared in version 1.11.8. If disabled, redirects issued by nginx will be relative. See also server_na

nginx.org

 

uri에서 redirectKeyword라는 문자열이 있으면 이동하는 설정

# nginx variable $request_uri
# full original request URI (with arguments)
# http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri

if ( $request_uri ~* "(.*)redirectKeyword(.*)" ) {
	return 301 $1uri.change.result$2;
}


# path에서 replace 처리 
http://karint.com/go/redirectKeyword/bbb/auq?lang=en&url=qqqq&ddd=aaaaaa
$1: http://karint.com/go/
$2: /bbb/auq?lang=en&url=qqqq&ddd=aaaaaa
==>  http://karint.com/go/uri.change.result/bbb/auq?lang=en&url=qqqq&ddd=aaaaaa

# parameter에서 replace 처리 
http://karint.com/go/bbb/auq?lang=en&url=redirectKeyword&ddd=aaaaaa
$1: http://karint.com/go/bbb/auq?lang=en&url=
$2: &ddd=aaaaaa

=> http://karint.com/go/bbb/auq?lang=en&url=uri.change.result&ddd=aaaaaa

 

 

parameter에서 redirectKeyword문자열이 있으면 이동시키는 소스 

#parameter에서 replace 설정

if ( $args ~* "(.*)redirectKeyword(.*)" ) {
	return 301 $uri?$1arg.change.result$2;
}

http://karint.com/go/bbb/auq?lang=en&url=redirectKeyword&ddd=aaaaaa
$1: lang=en&url=
$2: &ddd=aaaaaa

=> lang=en&url=uri.change.result&ddd=aaaaaa

 

 

아래 설정은 uri 와 parameter 에서 redirectKeyword라는 문자열이 오면 uri를 이동시키는 설정이다.

server {
    server_name hulint.local;

    #uri(path + parameter) rewrite
    if ( $request_uri ~* "(.*)redirectKeyword(.*)" ) {
        return 301 $1uri.change.result$2;
    }
	
    #parameter에서 rewrite
    if ( $args ~* "(.*)redirectKeyword(.*)" ) {
        return 301 $uri?$1arg.change.result$2;
    }
	
    location / {
        root C:/work/source/hulint;
    }
	
}
반응형

'linux' 카테고리의 다른 글

gradlew 프록시 설정  (0) 2021.01.13
bash shell 실행 명령어 확인하기  (0) 2020.08.20
diff를 이용한 파일 비교  (0) 2020.05.08
redis 5.0.5 설치  (0) 2019.08.20
NGINX IP 필터링  (0) 2018.06.22