/usr/local/Cellar/go/1.5.1/libexec/src/
他的RemoteAddr 是从哪里获取?
func (c *conn) RemoteAddr() Addr {
if !c.ok() {
return nil
}
return c.fd.raddr
}
使用远程地址的被nginx代理后获取不正确,都为127.0.0.1,解决方案需要配置nginx
server { listen ; server_name localhost; location /{ root html; index index.html index.htm; proxy_pass http://backend; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-For $http_x_forwarded_for; } 方案1 nginx这里作一个赋值操作,如下: proxy_set_header X-real-ip $remote_addr; 其中这个X-real-ip是一个自定义的变量名,名字可以随意取,这样做完之后,用户的真实ip就被放在X-real-ip这个变量里了,然后,在web端可以这样获取:
以前的头 head map 有
map[User-Agent:[Apache-HttpClient/4.2.6 (java 1.5)] Connection:[keep-alive] Content-Type:[application/json] Accept-Encoding:[gzip] Content-Length:[99]]
添加配置后就有
: map[Connection:[close] Content-Length:[99] Content-Type:[application/json] Accept-Encoding:[gzip] User-Agent:[Apache-HttpClient/4.2.6 (java 1.5)] X-Real-Ip:[192.168.100.103] X-Forwarded-For:[192.168.100.103]]