本地浏览器访问云服务器127.0.0.1的某个端口

目前在学习 gin 框架,但是在无图形化界面的 linux 云服务器上看不到网页的效果,而且 lynx 等纯文本浏览器过于丑陋了。于是研究了一下如何在本地计算机访问云服务器上的 127.0.0.1(感谢 new bing)

首先简单写了抄了个 go 语言编写的 http服务器,在 127.0.0.1:9090 监听,并相应 /hello 的请求,返回 hello.txt 的内容

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
	b, _ := ioutil.ReadFile("./hello.txt")
	_, _ = fmt.Fprintln(w, string(b))
}

func main() {
	http.HandleFunc("/hello", sayHello)
	err := http.ListenAndServe("127.0.0.1:9090", nil)
	if err != nil {
		fmt.Println("http serve failed, err:", err)
		return
	}
}

下面为在 127.0.0.1:9090 部署的网页

<h1 style = 'color:orange'>Hello Golang!</h1>
<h2>how are you?</h2>
<img id = 'i1' src = 'https://img2.baidu.com/it/u=1433204108,3228101951&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1678640400&t=d2bebd9853a910cefc6ae9cb1d48acf7'>

但是由于我的 linux 服务器无图形化界面,于是想在本地计算机的浏览器访问到云服务器的本地回环地址,由于本地的 ~/.ssh/config 已经配置好了云服务器的别名(ssh0)和私钥文件位置,在终端输入
ssh ssh0 -L 127.0.0.1:8888:127.0.0.1:9090
ssh0 是云服务器别名
前两个是本地的ip和端口
后两个是云服务器的ip和端口
可以把云服务器的ip和端口映射到本地,因此在本地的 127.0.0.1:8888 来访问云服务器的 127.0.0.1:9090
image

posted @ 2023-03-11 20:57  hzy0227  阅读(759)  评论(0编辑  收藏  举报