server { charset utf-8; client_max_body_size 128M; listen 80; ## listen for ipv4 #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 index test.htm; root /tmp; server_name t.cn; rewrite ^(.*) /$host$uri last; location / { proxy_pass http://goserver; } } upstream goserver { server 127.0.0.1:9205; }
package main import ( "fmt" "github.com/gin-gonic/gin" "net/http" _ "net/http" ) func main () { defer func(){ // 必须要先声明defer,否则不能捕获到panic异常 if err:=recover();err!=nil{ fmt.Println(err) } }() // 初始化引擎 engine := gin.Default() // 注册一个路由和处理函数 engine.Any("/", WebRoot) tt := engine.Group("/t.cn") { tt.GET("/one", func(context *gin.Context) { context.String(http.StatusOK, "haha") }) } // 绑定端口,然后启动应用 engine.Run(":9205") } func WebRoot(context *gin.Context) { context.String(http.StatusOK, "hello, world") }