nginx 에서 if 구문을 중첩해서 사용하는 방법
페이지 정보

본문
** 참고 : https://sub0709.tistory.com/69
## nginx 에서 if 구문을 아래처럼 사용할 수 없다.
```
if ($uri = '특정URI' && $request_method = 'POST') {
}
if ($uri = '특정URI') {
if ($request_method = 'POST') {
}
}
```
## 해결방법
아래와 같은 방식으로 사용할 수 있다. 꼼수.
```
if ($uri = '특정URI') {
set $STEP A;
}
if ($request_method = 'POST') {
set $STEP "${STEP}B";
}
if ($STEP = 'AB') {
return 404;
}
```
## 해결방법 2
** 참고 : https://akageun.github.io/2018/01/12/nginx-multiple-if-statements.html
```
// 이렇게 할 수 없다.
//if ($host = 'a.example.com' || $host = 'b.example.com'){
// return 301 https://$host$request_uri;
//}
set $is_redirect_val 0;
if ($host = 'a.example.com') {
set $is_redirect_val 1;
}
if ($host = 'b.example.com') {
set $is_redirect_val 1;
}
if ($is_redirect_val = 1) {
return 301 https://$host$request_uri;
}
```
## nginx 에서 if 구문을 아래처럼 사용할 수 없다.
```
if ($uri = '특정URI' && $request_method = 'POST') {
}
if ($uri = '특정URI') {
if ($request_method = 'POST') {
}
}
```
## 해결방법
아래와 같은 방식으로 사용할 수 있다. 꼼수.
```
if ($uri = '특정URI') {
set $STEP A;
}
if ($request_method = 'POST') {
set $STEP "${STEP}B";
}
if ($STEP = 'AB') {
return 404;
}
```
## 해결방법 2
** 참고 : https://akageun.github.io/2018/01/12/nginx-multiple-if-statements.html
```
// 이렇게 할 수 없다.
//if ($host = 'a.example.com' || $host = 'b.example.com'){
// return 301 https://$host$request_uri;
//}
set $is_redirect_val 0;
if ($host = 'a.example.com') {
set $is_redirect_val 1;
}
if ($host = 'b.example.com') {
set $is_redirect_val 1;
}
if ($is_redirect_val = 1) {
return 301 https://$host$request_uri;
}
```
추천0
댓글목록
등록된 댓글이 없습니다.