소개/소소한공부

Cloudflare Tunnel 설정 및 자동 실행 방법 보안 웹 서버 구축 가이드

이영훈닷컴 2025. 4. 11. 14:00
728x90

목적

서버를 외부에 노출하지 않고도 Cloudflare를 통해 안전하게 접속할 수 있도록 Cloudflare Tunnel을 설정한다.

설치 및 기본 설정

Cloudflare 저장소 및 GPG 키 등록

sudo mkdir -p --mode=0755 /usr/share/keyrings 
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

패키지 소스 등록 & cloudflared 설치

echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list 
sudo apt-get update && sudo apt-get install cloudflared

Cloudflare 로그인 및 터널 생성

Cloudflare 계정 인증 → 인증 링크 클릭해서 Cloudflare 계정에 연결

cloudflared tunnel login

터널 생성
→ 터널 생성 시 <Tunnel-ID>가 출력됨
→ 인증 파일이 자동으로 /home/<사용자명>/.cloudflared/<Tunnel-ID>.json에 저장됨

cloudflared tunnel create my-tunnel

구성 파일 설정

구성 파일 작성
경로: /etc/cloudflared/config.yml

tunnel: <Tunnel-ID> 
credentials-file: /home/taecheonin/.cloudflared/<Tunnel-ID>.json

터널 실행

터널 실행 (수동)

cloudflared tunnel run my-tunnel

재부팅 시 자동 실행 설정

서비스 설치 및 활성화

    sudo cloudflared service install
    sudo systemctl enable cloudflared
    sudo systemctl start cloudflared
    sudo systemctl status cloudflared

도메인 인그레스 설정 (옵션)

/etc/cloudflared/config.yml에 다음 블록 추가:

단일 호스트 설정 예시:

ingress:
  - hostname: leeyounghun.com
    service: http://localhost:8000
  - service: http_status:404

여러 도메인 처리 예시:

ingress:
  - hostname: example.com
    service: http://localhost:3000
  - hostname: www.example.com
    service: http://localhost:3000
  - service: http_status:404

기억할 점

  • Tunnel-ID.json 인증 파일은 절대 유출 금지
  • config.yml 경로는 /etc/cloudflared/
  • 자동 실행을 위해 cloudflared service install 필수
  • ingress 설정은 도메인 → 내부 서비스 매핑

필요하면 systemctl status cloudflared로 상태 확인 가능!

728x90