Files
taxbaik/NGINX_CONFIG_GUIDE.md
kjh2064 89fa51efe2
TaxBaik CI/CD / build-and-deploy (push) Failing after 2m20s
refactor admin UX and stabilize shell
2026-07-09 00:03:33 +09:00

8.5 KiB

Nginx 설정 변경 가이드 (TaxBaik 경로 최적화)

📍 설정 파일 위치

/etc/nginx/sites-available/taxbaik-domains.conf

🔍 현재 설정 확인

1단계: 운영 서버에 접속

ssh kjh2064@178.104.200.7

2단계: 현재 설정 확인

sudo cat /etc/nginx/sites-available/taxbaik-domains.conf

또는 특정 부분만:

sudo grep -A 15 "location /" /etc/nginx/sites-available/taxbaik-domains.conf

📋 현재 설정 (변경 전)

location / {
    proxy_pass         http://127.0.0.1:5001;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "Upgrade";
    proxy_cache_bypass  $http_upgrade;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_read_timeout 120s;
}

변경해야 할 부분

변경 1: location 경로 수정

- location /taxbaik {
+ location / {

변경 2: proxy_pass 수정

- proxy_pass         http://127.0.0.1:5001;
+ proxy_pass         http://127.0.0.1:5001/;

변경 3: rewrite 규칙 추가

루트 프록시에서는 rewrite가 필요하지 않습니다.


📝 변경 후 최종 설정

location / {
    proxy_pass         http://127.0.0.1:5001;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "Upgrade";
    proxy_cache_bypass  $http_upgrade;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_read_timeout 120s;
}

🛠️ 설정 변경 방법

방법 1: nano 에디터 (권장)

# 1. 백업 생성
sudo cp /etc/nginx/sites-available/taxbaik-domains.conf /etc/nginx/sites-available/taxbaik-domains.conf.backup

# 2. 편집
sudo nano /etc/nginx/sites-available/taxbaik-domains.conf

# 3. 다음 변경사항 적용:
#    - location /taxbaik { → location /
#    - proxy_pass http://127.0.0.1:5001; → proxy_pass http://127.0.0.1:5001;

# 4. 저장 후 종료
#    Ctrl+X → Y → Enter

방법 2: sed 명령어 (자동)

# 백업
sudo cp /etc/nginx/sites-available/taxbaik-domains.conf /etc/nginx/sites-available/taxbaik-domains.conf.backup

# 변경 1: location / 유지
sudo sed -i 's/location \/taxbaik {/location \//g' /etc/nginx/sites-available/taxbaik-domains.conf

# 변경 2: proxy_pass 유지
sudo sed -i 's|proxy_pass         http://127.0.0.1:5001/;|proxy_pass         http://127.0.0.1:5001;|g' /etc/nginx/sites-available/taxbaik-domains.conf

방법 3: Python 스크립트

# nginx_fix.py
import re

nginx_file = "/etc/nginx/sites-available/taxbaik-domains.conf"

# 백업
import shutil
shutil.copy(nginx_file, nginx_file + ".backup")

# 설정 읽기
with open(nginx_file, 'r') as f:
    content = f.read()

# 변경 1: location /taxbaik { → location /taxbaik/ {
content = content.replace('location /taxbaik {', 'location /taxbaik/ {')

# 변경 2: proxy_pass 수정
content = content.replace(
    'proxy_pass         http://127.0.0.1:5001;',
    'proxy_pass         http://127.0.0.1:5001/;'
)

# 변경 3: rewrite 규칙 추가 (한 번만)
if 'rewrite ^/taxbaik/' not in content:
    content = content.replace(
        'proxy_pass         http://127.0.0.1:5001/;',
        'proxy_pass         http://127.0.0.1:5001/;\n    rewrite ^/taxbaik/(.*)$ /$1 break;'
    )

# 설정 저장
with open(nginx_file, 'w') as f:
    f.write(content)

print("✅ Nginx 설정 변경 완료")

실행:

sudo python3 nginx_fix.py

✓ 설정 변경 후 검증

Step 1: 문법 검증

sudo nginx -t

예상 출력:

nginx: the configuration file /etc/nginx/sites-available/taxbaik-domains.conf syntax is ok
nginx: configuration test is successful

오류가 나면:

  • 백업에서 복구: sudo cp /etc/nginx/sites-available/taxbaik-domains.conf.backup /etc/nginx/sites-available/taxbaik-domains.conf
  • 다시 변경 시도

Step 2: Nginx 재로드

sudo systemctl reload nginx

또는

sudo systemctl restart nginx

상태 확인:

sudo systemctl status nginx

Step 3: 포트 확인

# Nginx가 포트 80, 443을 사용하는지 확인
sudo netstat -tlnp | grep nginx

예상 출력:

tcp        0      0 0.0.0.0:80                 0.0.0.0:*               LISTEN      1234/nginx
tcp        0      0 0.0.0.0:443                0.0.0.0:*               LISTEN      1234/nginx

Step 4: 웹사이트 테스트

# 공개 사이트
curl -I http://www.taxbaik.com/taxbaik/
# 예상: HTTP 200

# 관리자 로그인
curl -I http://www.taxbaik.com/taxbaik/admin/login
# 예상: HTTP 200

# API
curl -I http://www.taxbaik.com/taxbaik/api/auth/login
# 예상: HTTP 405 (POST only) 또는 HTTP 200

📊 변경 전후 비교

요청 흐름 (변경 전)

공개 URL: http://www.taxbaik.com/taxbaik/admin/login
    ↓
Nginx (location /taxbaik): proxy_pass http://127.0.0.1:5001
    ↓
앱 수신: http://127.0.0.1:5001/taxbaik/admin/login
    ↓
앱 경로: /taxbaik/admin/login ← PathBase 없음 → 경로 미인식
    ↓
❌ 404 오류

요청 흐름 (변경 후)

공개 URL: http://www.taxbaik.com/taxbaik/admin/login
    ↓
Nginx (location /taxbaik/): rewrite 규칙
    ↓
변환: /admin/login (taxbaik 제거)
    ↓
앱 수신: http://127.0.0.1:5001/admin/login
    ↓
앱 경로: /admin/login ← base href="/taxbaik/admin/" 적용
    ↓
브라우저: /taxbaik/admin/dashboard (상대경로로 변환)
    ↓
✅ 정상 작동

🔙 롤백 (문제 발생 시)

즉시 롤백

# 백업 복구
sudo cp /etc/nginx/sites-available/taxbaik-domains.conf.backup /etc/nginx/sites-available/taxbaik-domains.conf

# Nginx 재로드
sudo systemctl reload nginx

# 확인
sudo systemctl status nginx

변경 내용 확인

변경 전 내용 확인:

sudo diff /etc/nginx/sites-available/taxbaik-domains.conf.backup /etc/nginx/sites-available/taxbaik-domains.conf

🆘 문제 해결

문제 1: nginx -t 오류

증상:

nginx: [emerg] unknown directive "rewrite" in /etc/nginx/sites-available/taxbaik-domains.conf:XX

해결:

  • rewrite 문법 확인: rewrite ^/taxbaik/(.*)$ /$1 break; (정확하게)
  • 백업에서 복구 후 다시 시도

문제 2: 설정 적용 안 됨

# Nginx 완전 재시작
sudo systemctl stop nginx
sudo systemctl start nginx

# 또는
sudo nginx -s reload

문제 3: 여전히 404

# 1. 설정 재확인
sudo cat /etc/nginx/sites-available/taxbaik-domains.conf | grep -A 3 "location /taxbaik"

# 2. Nginx 로그 확인
sudo tail -100 /var/log/nginx/error.log

# 3. 앱 로그 확인
sudo systemctl status taxbaik
sudo journalctl -u taxbaik -n 50

# 4. DNS 확인
nslookup www.taxbaik.com 8.8.8.8

📋 체크리스트

배포 전:

  • 운영 서버에 ssh 접속 확인
  • 현재 설정 백업 생성
  • 변경 계획 검토

배포:

  • location /taxbaik → location /taxbaik/ (변경 1)
  • proxy_pass 수정 (변경 2)
  • rewrite 규칙 추가 (변경 3)
  • nginx -t 문법 검증 성공
  • systemctl reload nginx 실행

검증:


💡

  1. 변경 전 항상 백업

    sudo cp /etc/nginx/sites-available/taxbaik-domains.conf /etc/nginx/sites-available/taxbaik-domains.conf.backup
    
  2. nano에서 수정할 때:

    • Ctrl+W → "location /taxbaik" 검색
    • Ctrl+X → Y → Enter 저장
  3. 변경 확인:

    sudo diff -u /etc/nginx/sites-available/taxbaik-domains.conf.backup /etc/nginx/sites-available/taxbaik-domains.conf
    
  4. 전체 설정 보기:

    sudo cat /etc/nginx/sites-available/taxbaik-domains.conf | grep -v "^#" | grep -v "^$"
    

변경 완료 후 운영 서버 확인:

# 최종 검증
echo "=== 설정 확인 ===" && \
sudo grep -A 5 "location /taxbaik" /etc/nginx/sites-available/taxbaik-domains.conf && \
echo "" && \
echo "=== 문법 검증 ===" && \
sudo nginx -t && \
echo "" && \
echo "=== Nginx 상태 ===" && \
sudo systemctl status nginx --no-pager

완료!