利用nginx + reverse ssh tunnel 架設臨時對外網頁服務

 我有一台內部區網的測試用網頁伺服器,因為有需要由外網來測試網頁,所以我是用ngrok來讓外網可以連到內網的測試網頁。

最近在網路上看到reverse ssh tunnel的技術文件,加上我在Cloud上有一台nginx的反向代理伺服器,所以就順著文件來模擬ngrok。

公開的網址是:test1.twtnn.com

內部的網址是:http://192.168.10.10:2222

在ngrok的設定檔新增一個設定如下:

server {
  server_name test1.twtnn.com;
  location / {
      proxy_pass http://localhost:17777/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

其中proxy_pass就是當連線test1.twtnn.com時會轉送到http://localhost:17777

如果nginx與你的local server間使用的 https則可以加上proxy_set_header X-Forwarded-Proto https; 

接著在內網的伺服器設定reverse ssh tunnel,因為Cloud是用private key登入的,所以要加上-i

$ ssh -R 17777:localhost:2222 -i privte.key account@test1.twtnn.com   

-R可以讓test1.twtnn.com上面的port:17777反向連入內部本機的port:2222

接著在瀏覽器開啟網址:http://test1.twtnn.com,就可以瀏覽內部的網頁了。



Windows 11安裝時跳過網路連線