Home / golang

Golang Gin & Nginx to Get Real Ip of Visitors

Posted on:2020-04-23 Views:10703 Words:55

In the golang gin controller

c.ClientIP()

Gets the IP of the request, but gets 127.0.0.1.

Presumably Nginx was not configured when it forwarded the request to golang process.

Solutions

Just add two lines to the configuration.

location /go/ {
        proxy_set_header X-Forward-For $remote_addr;
        proxy_set_header X-real-ip $remote_addr;
        proxy_pass http://127.0.0.1:8080/go/;
}

reload nginx, test, OK.