Chomu's Blog.

>

Posts

GitHub

Cloudflare로 로컬서버 호스팅하기

맥 기준

목차

초기 설정

Cloudflare 계정 생성 ~ 도메인 구매

여기까지는 되어 있다고 가정

cloudflared

cloudflared: Cloudflare Tunnel 클라이언트 CLI

설치(homebrew 는 설치되어 있다고 가정)

brew install cloudflared

맥 외의 OS 는 여기서 확인

Cloudflare 로그인

cloudflared tunnel login

브라우저가 열리면서 Cloudflare 계정에 로그인 창이 뜨면 로그인

Tunnel 생성

cloudflared tunnel create <터널>
# Tunnel credentials written to /Users/<유저 이름>/.cloudflared/<터널 ID>.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.\
#
# Created tunnel <터널 이름> with id <터널 ID>

plist 파일 생성

컴퓨터가 재부팅 되거나 프로세시가 종료됐을 때마다 자동으로 cloudflared tunnel run <터널 이름> 를 재실행하기 위한 plist 파일을 생성하는 과정. 매번 수동으로만 할거라면 필요 없음.(그게 더 안전하기도 하고) 상기했다시피 맥 기준이고 그 외의 OS 라면 시작 프로그램으로 cloudflared tunnel run <터널 이름> 커맨드가 실행되도록 설정하면 됨.

# 파일 이름은 좀 달라도 됨 중요한 것 아님
sudo touch ~/Library/LaunchAgent/com.user.cloudflared.plist

본인이 편한 IDE로 열어서 아래 내용을 붙여넣기

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key> <!-- 파일 이름 -->
    <string>com.user.cloudflared</string>
    <key>ProgramArguments</key> <!-- 실행할 커맨드 -->
    <array>
        <!--
          cloudflared 경로
          homebrew 로 설치한 거라 경로가 다를 수도 있음
          확인은 `which cloudflared` 로 확인 가능
          터널 이름은 위에서 생성한 터널 이름로 변경해야 함
        -->
        <string>/opt/homebrew/bin/cloudflared</string>
        <string>tunnel</string>
        <string>run</string>
        <string>터널 이름</string>
    </array>
    <key>RunAtLoad</key> <!-- 컴퓨터가 켜질 때 자동으로 실행 -->
    <true/>
    <key>KeepAlive</key> <!-- 프로세스가 죽었을 때 자동으로 재실행 -->
    <true/>
    <key>StandardOutPath</key> <!-- 로그 파일 경로 -->
    <string>/tmp/cloudflared.log</string>
    <key>StandardErrorPath</key> <!-- 에러 로그 파일 경로 -->
    <string>/tmp/cloudflared.error.log</string>
</dict>
</plist>

새로운 도메인 추가 시

config.yml 파일 수정

# ~/.cloudflared/config.yml
tunnel: <터널 ID>
credentials-file: <터널 credentials 파일 경로>
ingress:
  - hostname: <도메인1>
    service: http://localhost:<포트1>
  - hostname: <도메인2>
    service: http://localhost:<포트2>
  # ...
  - service: http_status:404

cloudflared 재시작

# plist 파일을 사용하고 있다면
sudo launchctl unload ~/Library/LaunchAgents/com.user.cloudflared.plist
sudo launchctl load ~/Library/LaunchAgents/com.user.cloudflared.plist
# 터널을 수동으로 실행하고 있다면
cloudflared tunnel run <터널>

터널 생성 없이 Cloudflare Access 를 통해 로컬 서버에 접근하기

cloudflared access tcp --hostname <도메> --url http://localhost:<>

이 커맨드는 터널을 생성하지 않고도 Cloudflare Access 를 통해 로컬 서버에 접근할 수 있도록 해줌.