Golang Gin & Nginx to Get Real Ip of Visitors

Updated at: 2020-04-23 Views: 1825 Words: 55 Category: golang

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.

Leave a Comment