配置 NginX :

  location / {
    proxy_pass http://127.0.0.1:8080;

    resolver_timeout 30s;

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

然后使用 Header.Get("X-Real-Ip") 获得真实的IP:


func NewRequest(r *http.Request) *Request {
       ...
	request.RemoteAddr = r.Header.Get("X-Real-Ip")
       ...
}