Skip to content

Commit

Permalink
feature: api서버의 로그 분리 (#77)
Browse files Browse the repository at this point in the history
- api서버의 요청에 대해 별개의 log 파일을 생성하도록 변경
- api서버 log만 읽어서 응답을 제공
- Nginx 가 api 서버 log 파일을삭제할 수 있는 권한 제공
  • Loading branch information
LuizyHub authored Aug 17, 2024
1 parent e9443a4 commit 903abbb
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions nginx/sites-available/default
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ server {
server_name www.logbat.info logbat.info api.logbat.info sdk.logbat.info view.logbat.info nginx.logbat.info;

return 301 https://$host$request_uri; # HTTP 요청을 HTTPS로 리디렉션
# managed by Certbot
}

server {
Expand Down Expand Up @@ -33,6 +32,9 @@ server {

include /etc/nginx/snippets/private-ip.conf;

access_log /var/log/nginx/api_access.log; # api.logbat.info 전용 액세스 로그 파일
error_log /var/log/nginx/api_error.log; # api.logbat.info 전용 에러 로그 파일

location /logs {
proxy_pass $logbat_server;
proxy_set_header Host $host;
Expand Down Expand Up @@ -124,12 +126,12 @@ server {
location /count {
content_by_lua_block {
-- access.log의 라인 수를 세어 successCount로 사용
local handle_access = io.popen("wc -l /var/log/nginx/access.log | cut -d' ' -f1")
local handle_access = io.popen("wc -l < /var/log/nginx/api_access.log")
local successCount = handle_access:read("*a")
handle_access:close()

-- error.log의 라인 수를 세어 errorCount로 사용
local handle_error = io.popen("wc -l /var/log/nginx/error.log | cut -d' ' -f1")
local handle_error = io.popen("wc -l < /var/log/nginx/api_error.log")
local errorCount = handle_error:read("*a")
handle_error:close()

Expand All @@ -142,8 +144,8 @@ server {
location /count/reset {
content_by_lua_block {
-- 로그 파일 지우기
os.execute("sudo truncate -s 0 /var/log/nginx/access.log")
os.execute("sudo truncate -s 0 /var/log/nginx/error.log")
os.execute("sudo truncate -s 0 /var/log/nginx/api_access.log")
os.execute("sudo truncate -s 0 /var/log/nginx/api_error.log")

-- 204 상태 코드 반환
ngx.status = ngx.HTTP_NO_CONTENT
Expand Down

0 comments on commit 903abbb

Please sign in to comment.