Skip to content

Instantly share code, notes, and snippets.

@anxgang
Last active June 8, 2024 08:01
Show Gist options
  • Save anxgang/938092dc998e5445b5d94f764705efa3 to your computer and use it in GitHub Desktop.
Save anxgang/938092dc998e5445b5d94f764705efa3 to your computer and use it in GitHub Desktop.
替代 ngrok 方式

替代 ngrok 方式

1. nginx 建立

server {
  server_name xxx.yourdomain.com;
  location / {
      proxy_pass http://localhost:3333/;
      proxy_set_header X-Real-IP $remote_addr;                     # 把真實的 client ip 放到 X-Real-IP
      proxy_set_header Host $host;                                 # 將 Host 指定為 xxx.yourdomain.com
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 將 X-Forwarded-For 加上這台 nginx,記錄所有轉送的伺服器
      proxy_set_header X-Forwarded-Proto https;                    # 設定 nginx 與 你的 local server 間使用的 http 協議,如果有設定 https 要加上這行
  }
}

2. 由 client 輸入指令 透過 server 3333 port 反向代理 localhost:3000

ssh -NfR 3333:localhost:3000 [email protected]

3. 不想每次連接都輸入密碼可以複製 ssh-key 上去

ssh-copy-id [email protected]

4. 透過 certbot 或 cloudflare 安裝 https

certbot

https://certbot.eff.org/

sudo apt update
sudo apt install snapd
sudo snap install --classic certbot
sudo certbot --nginx
sudo certbot renew --dry-run

cloudflare

  • DNS 設定一組 xxx.yourdomain.com 並選 『透過 proxy 處理』
  • SSL/TLS
    • 加密模式 - 設定『flexible(彈性)』以上
    • 邊緣憑證 - 一律使用 HTTPS

補充

反向代理

ssh -NfR [remote_port]:locahost:[local_port] [email protected]
  • -C:壓縮資料傳輸。
  • -f:後臺認證使用者/密碼,通常和-N連用,不用登入到遠端主機。
  • -N:不執行指令碼或命令,通常與-f連用。
  • -g:在-L/-R/-D引數中,允許遠端主機連線到建立的轉發的埠,如果不加這個引數,只允許本地主機建立連線。
  • -L:本地埠:目標IP:目標埠
  • -R:本地埠:目標IP:目標埠

正向代理

ssh -NfL [local_port]:localhost:[remote_port]

在 zsh 建立 alias

# 已指定 port, 例如: ng
alias ng="f() { ssh -CNfR 3333:localhost:3000 [email protected]; echo 'localhost:3000 -> https://xxx.yourdomain.com \n' };f"
# 可輸入 port  例如: ngp 3000
alias ngp='f() { ssh -CNfR 3333:localhost:$1 [email protected]; echo "localhost:$1 -> https://xxx.yourdomain.com \n" };f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment