Golang – net/http – How to get client IP address (Real-IP)

Spread the love

For Nginx need to add in location:

proxy_set_header X-Real-IP $remote_addr:$server_port;

In Golang code need to add function:

func GetRealIP(r *http.Request) string {
    IPAddress := r.Header.Get("X-Real-IP")
    if IPAddress == "" {
        IPAddress = r.Header.Get("X-Forwarder-For")
    }
    if IPAddress == "" {
        IPAddress = r.RemoteAddr
    }
    return IPAddress
}

Добавить комментарий