[NGINX] 간단한 설정과 auth request
목차
- 파일 설명
- sites ~
- auth_request
- $http_name 사용자 정의 헤더 값
- /etc/nginx/nginx.conf: 글로벌 설정을 관리하는 메인 설정 파일.
- /etc/nginx/conf.d/*.conf: 개별 서버 블록 설정 파일들이 위치하는 디렉토리.
- /etc/nginx/sites-available/: 개별 사이트의 설정 파일들이 위치하는 디렉토리.
- /etc/nginx/sites-enabled/: 활성화된 사이트의 설정 파일들이 위치하는 디렉토리.
- /etc/nginx/mime.types: MIME 타입 설정 파일.
sites-available, sites-enabled 에 대하여 ...
해당 설정은 한 웹 서버에서 여러 다른 도메인으로 서비스를 띄울 때 유용하다.
test1.com
tests2.com
위와 같이 2개의 도메인이 있을 때, available에 도메인 별 설정 파일을 만들고
아래와 같이 sites-available/test1.com 파일을 sites-enabled/test1.com으로 심볼릭 링크를 만들면 활성화된다.
sudo ln -s /etc/nginx/sites-available/test1.com /etc/nginx/sites-enabled/
사실 지금 하는 프로젝트 구성이 한 웹서버에 한 도메인이라 이렇게 안했다.
이론상 이렇게 하면 된다 정리만..
설정 파일 내용은 아래 적은 conf.d/default.conf 내용이랑 거의 똑같은듯 ?
나중에 적용할 수 있길
nginx.conf 파일에 아래 설정을 해야 /conf.d/*.conf, sites-enabled/* 파일들이 nginx에 적용됨
http {
# 다른 설정들...
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
nginx/conf.d/default.conf
default.conf에 설정 몰빵했는데 다음 구성 땐 좀 더 공부해봐야겠다.
server {
listen 80;
server_name abc.services.co.kr;
access_log /var/log/nginx/host.access.log custom;
location / {
root /usr/share/nginx/html;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
}
location /auth_check {
internal;
proxy_pass http://10.10.10.10:8081/auth;
proxy_set_header Content-Length "";
}
location @error403 {
return 302 http://abc.services.com/login;
}
location ^~/test1/ {
auth_request /auth_check;
auth_request_set $auth_cookie $upstream_http_set_cookie;
add_header Set-Cookie $auth_cookie;
...
proxy_pass http://10.10.10.10:8081;
}
location ^~/test2/ {
auth_request /auth_check;
auth_request_set $auth_cookie $upstream_http_set_cookie;
add_header Set-Cookie $auth_cookie;
...
proxy_pass http://10.10.10.10:8082;
}
}
일단 이 설정에서 중요한(?) 것은 auth_request
auth_request가 선언되면 클라이언트가 요청한 api는 auth_request를 먼저 통과해야한다
여기서는 auth request에 선언된 10.10.10.10:8081/auth를 먼저 통과해야된다.
auth request에서 401, 403 등을 반환하면 클라이언트가 요청한 api로 가는 길은 차단 ~
클라이언트에서 요청하는 api는 get,put, post, delete 등 다양하고 Content-Type도 application/json 등 다양하다.
auth_request는 (대부분) get요청이기 때문에 Content-Type이 따로 없다.
근데 auth_request를 호출 할 때 원본 요청 api Content-Type이 auth request에도 고대로 전해지는 것 같다.
auth_request에는 Content-Type이 가면 안되는데 자꾸 application/json 같은 게 가니까 request 500을 반환한다.
proxy_set_header Content-Length "";
이걸 설정해 Content-Type 설정을 없애면 된다.!!!!!
auth_request_set $auth_cookie $upstream_http_set_cookie;
이건 $auth_cookie라는 변수에 auth request에서 반환하는 set-cookie 값을 담는 것이다.
add_header Set-Cookie $auth_cookie;
그리고 client에 set-cookie 값을 넘겨줌 ~
근데 nginx에서 set cookie 값이 여러개 들어와도 하나밖에 못한댄다.. ㅜ
클라이언트에서 헤더를 커스텀 했다고 가정해보자.
헤더에 username, userid를 추가했다.
nginx에서 쓰고 싶다면?????
$http_username, $http_userid 이런 식으로 사용하면 된다.
로그 찍을 때 써도 되고
proxy_set_header X-Forwarded-UserID $http_userid;
이런식으로 proxy header에 명시해서 보내도 된다.