Skip to main content

Nginx에서 기본 인증 걸기

등록일: 2025. 6. 10. 3:59


[Nginx Basic 인증 설정 가이드 - whisper.sleepzz.xyz 전용] ​ 이 가이드는 Docker로 구성된 Nginx에서 whisper.sleepzz.xyz 도메인에 HTTP Basic 인증을 적용하는 방법을 설명합니다. 설정 대상은 다음과 같습니다: ​

  • Nginx Docker 컨테이너
  • 도메인: whisper.sleepzz.xyz
  • 인증 파일 경로: /data001/nginx/auth/whisper.htpasswd
  • 컨테이너 내부 경로: /etc/nginx/auth/whisper.htpasswd ​

  1. apache2-utils 설치

​ 먼저 htpasswd 명령어를 제공하는 패키지를 설치합니다. ​

sudo apt install apache2-utils

  1. 인증 파일 디렉토리 생성 및 htpasswd 파일 만들기

​ 호스트 경로 /data001/nginx/auth/ 아래에 인증 정보를 저장할 디렉토리를 생성합니다. ​

mkdir -p /data001/nginx/auth
htpasswd -c /data001/nginx/auth/whisper.htpasswd [id]

  • [id]에는 사용할 사용자 ID를 입력합니다.
  • 비밀번호는 프롬프트에서 입력됩니다.
  • sudo 없이도 생성 가능한 이유: 이 경로는 Docker의 볼륨으로 연결된 사용자 관리 디렉토리이기 때문입니다. ​ 추가 사용자 등록 시에는 -c를 생략합니다. ​
htpasswd /data001/nginx/auth/whisper.htpasswd [another_id]

  1. whisper.conf에 인증 설정 추가

/data001/nginx/conf.d/whisper.conf 파일을 다음과 같이 수정하거나 새로 생성합니다. ​

server {
listen 80;
server_name whisper.sleepzz.xyz;

location / {
auth_basic "Restricted Whisper Access";
auth_basic_user_file /etc/nginx/auth/whisper.htpasswd;

proxy_pass [http://172.17.0.1:7860](http://172.17.0.1:7860);
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;
}
}

  1. docker-compose.yaml에 볼륨 마운트 추가

​ nginx 서비스에 아래 항목을 volumes에 추가합니다. ​

- /data001/nginx/auth:/etc/nginx/auth:ro

​ 최종 예시: ​

services:
nginx:
image: nginx:latest
container_name: nginx-sleepzz-xyz
restart: unless-stopped
ports:
- "8080:80"
volumes:
- /sorc001:/sorc001
- /data001/nginx/conf.d:/etc/nginx/conf.d
- /data001/nginx/nginx.conf:/etc/nginx/nginx.conf
- /data001/nginx/log:/var/log/nginx
- /data001/nginx/auth:/etc/nginx/auth:ro
networks:
- cfd-net

  1. Docker Compose로 반영

​ docker-compose.yaml에서 볼륨을 수정했기 때문에 단순 재시작으로는 반영되지 않습니다. ​

docker-compose down
docker-compose up -d

  1. 접속 테스트

​ 브라우저에서 아래 주소로 접속합니다: ​

http://whisper.sleepzz.xyz:8080

  • 브라우저에서 로그인 창이 뜨고, 입력한 ID/PW로 정상 접근되면 설정 완료입니다.