From ebda875371ff17dc96e89a6d9fe66b6273e988bb Mon Sep 17 00:00:00 2001 From: kjh2064 Date: Wed, 8 Jul 2026 16:31:04 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B6=94=EA=B0=80:=20Nginx=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EB=B3=80=EA=B2=BD=20=EC=83=81=EC=84=B8=20=EA=B0=80?= =?UTF-8?q?=EC=9D=B4=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 가이드 내용 ✅ 현재 설정 확인 (before) ✅ 변경해야 할 3가지 항목 ✅ 최종 설정 (after) ✅ 3가지 변경 방법 - nano 에디터 (권장) - sed 명령어 (자동) - Python 스크립트 ✅ 검증 (4단계) ✅ 변경 전후 비교 ✅ 롤백 방법 ✅ 문제 해결 ## 파일 위치 /etc/nginx/sites-available/taxbaik-domains.conf ## 3가지 변경사항 1. location /taxbaik { → location /taxbaik/ { 2. proxy_pass http://127.0.0.1:5001; → proxy_pass http://127.0.0.1:5001/; 3. rewrite ^/taxbaik/(.*)$ /$1 break; (추가) Co-Authored-By: Claude Haiku 4.5 --- NGINX_CONFIG_GUIDE.md | 416 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 416 insertions(+) create mode 100644 NGINX_CONFIG_GUIDE.md diff --git a/NGINX_CONFIG_GUIDE.md b/NGINX_CONFIG_GUIDE.md new file mode 100644 index 0000000..c0ababd --- /dev/null +++ b/NGINX_CONFIG_GUIDE.md @@ -0,0 +1,416 @@ +# Nginx 설정 변경 가이드 (TaxBaik 경로 최적화) + +## 📍 설정 파일 위치 + +``` +/etc/nginx/sites-available/taxbaik-domains.conf +``` + +--- + +## 🔍 현재 설정 확인 + +### 1단계: 운영 서버에 접속 + +```bash +ssh kjh2064@178.104.200.7 +``` + +### 2단계: 현재 설정 확인 + +```bash +sudo cat /etc/nginx/sites-available/taxbaik-domains.conf +``` + +또는 특정 부분만: + +```bash +sudo grep -A 15 "location /taxbaik" /etc/nginx/sites-available/taxbaik-domains.conf +``` + +--- + +## 📋 현재 설정 (변경 전) + +```nginx +location /taxbaik { + 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 경로 수정 + +```diff +- location /taxbaik { ++ location /taxbaik/ { +``` + +### 변경 2: proxy_pass 수정 + +```diff +- proxy_pass http://127.0.0.1:5001; ++ proxy_pass http://127.0.0.1:5001/; +``` + +### 변경 3: rewrite 규칙 추가 + +```diff + location /taxbaik/ { + proxy_pass http://127.0.0.1:5001/; ++ rewrite ^/taxbaik/(.*)$ /$1 break; + proxy_http_version 1.1; +``` + +--- + +## 📝 변경 후 최종 설정 + +```nginx +location /taxbaik/ { + proxy_pass http://127.0.0.1:5001/; + rewrite ^/taxbaik/(.*)$ /$1 break; + 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 에디터 (권장) + +```bash +# 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 /taxbaik/ { +# - proxy_pass http://127.0.0.1:5001; → proxy_pass http://127.0.0.1:5001/; +# - proxy_pass 다음에 새 줄 추가: rewrite ^/taxbaik/(.*)$ /$1 break; + +# 4. 저장 후 종료 +# Ctrl+X → Y → Enter +``` + +### 방법 2: sed 명령어 (자동) + +```bash +# 백업 +sudo cp /etc/nginx/sites-available/taxbaik-domains.conf /etc/nginx/sites-available/taxbaik-domains.conf.backup + +# 변경 1: location /taxbaik → location /taxbaik/ +sudo sed -i 's/location \/taxbaik {/location \/taxbaik\/ {/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: rewrite 규칙 추가 +# (이 부분은 수동으로 확인 후 추가하는 것이 권장됨) +``` + +### 방법 3: Python 스크립트 + +```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 설정 변경 완료") +``` + +실행: + +```bash +sudo python3 nginx_fix.py +``` + +--- + +## ✓ 설정 변경 후 검증 + +### Step 1: 문법 검증 + +```bash +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 재로드 + +```bash +sudo systemctl reload nginx +``` + +또는 + +```bash +sudo systemctl restart nginx +``` + +**상태 확인:** +```bash +sudo systemctl status nginx +``` + +### Step 3: 포트 확인 + +```bash +# 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: 웹사이트 테스트 + +```bash +# 공개 사이트 +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 (상대경로로 변환) + ↓ +✅ 정상 작동 +``` + +--- + +## 🔙 롤백 (문제 발생 시) + +### 즉시 롤백 + +```bash +# 백업 복구 +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 +``` + +### 변경 내용 확인 + +변경 전 내용 확인: + +```bash +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: 설정 적용 안 됨 + +```bash +# Nginx 완전 재시작 +sudo systemctl stop nginx +sudo systemctl start nginx + +# 또는 +sudo nginx -s reload +``` + +### 문제 3: 여전히 404 + +```bash +# 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 실행 + +검증: + +- [ ] curl -I http://www.taxbaik.com/taxbaik/ → HTTP 200 +- [ ] curl -I http://www.taxbaik.com/taxbaik/admin/login → HTTP 200 +- [ ] 브라우저에서 실제 접속 확인 +- [ ] E2E 테스트 실행 + +--- + +## 💡 팁 + +1. **변경 전 항상 백업** + ```bash + 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. **변경 확인:** + ```bash + sudo diff -u /etc/nginx/sites-available/taxbaik-domains.conf.backup /etc/nginx/sites-available/taxbaik-domains.conf + ``` + +4. **전체 설정 보기:** + ```bash + sudo cat /etc/nginx/sites-available/taxbaik-domains.conf | grep -v "^#" | grep -v "^$" + ``` + +--- + +**변경 완료 후 운영 서버 확인:** + +```bash +# 최종 검증 +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 +``` + +--- + +**완료!** ✨