go nginx代理到go服务器

2017/11/30
go基础练习
go很简洁,像php一样好学
又有很强大的协程
值得好好学习下

代理

正文

配置nginx 代理
server
{
	listen 80;
	#listen [::]:80;
	server_name xxxx.com;
	root  /home/wwwroot/weixinjtypt/;

	location / {
		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 proxy_set_header Host $http_host;
		 proxy_redirect off;
		  proxy_pass http://localhost:12345;
	}

}


开启go服务器读取日志

package main

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

func main() {
        http.HandleFunc("/rizi", HelloServer)
        err := http.ListenAndServe(":12345", nil)
        if err != nil {
                log.Fatal("ListenAndServe: ", err)
        }
}

func HelloServer(w http.ResponseWriter, req *http.Request) {
        req.ParseForm()
        date, _ := req.Form["date"]
        setdate := date[0]
        filepath := "/home/wwwroot/server/Application/Runtime/Logs/Home/" + setdate + ".err"
        fmt.Println(filepath)
        file, _ := os.Open(filepath)
        datalist, _ := ioutil.ReadAll(file)
        io.WriteString(w, string(datalist))
		
		
		/*host := r.Host
		r.ParseForm() //默认不解析get参数。需要加这个函数
		var str string
		if len(r.Form) > 0 {
			for k, v := range r.Form {
				str += "当前的参数名:" + k + "=" + v[0] + " "
			}
		}

		b := []byte("helloWrold!欢迎来自:" + host + "用户\n" + str)
		_, err := w.Write(b)

		if err != nil {
			panic(err)
		}*/
}

go run  运行
然后读取TP日志文件,分析每天的错误信息

个人随笔

安静的等待,你若盛开,蝴蝶自来

承接高质量 网站开发 app开发 如有需要请点击About联系

@承鹏辉