W
W
wargych2020-08-30 16:19:58
Nginx
wargych, 2020-08-30 16:19:58

How to correctly transfer real ip from nginx to go-server?

Dobryi day
The request to understanding to prompt on two questions.
1) how to correctly transfer the real ip of users from nginx to the server with a websocket? tried these instructions:

https://dev.to/hackersandslackers/deploy-a-golang-...


https://serveradmin.ru/nginx-proxy_pass/#Peredaca_...

but so far it has not been possible to collect the config.
Now the desired location looks like this:
location = /game {
            proxy_pass http://localhost:9000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

2) remote ip in go I get like this:
user.conn.RemoteAddr().String()

https://pkg.go.dev/net?tab=doc#Addr

During tests on a local server, ip was issued normally, but on a combat server, sometimes it is issued normally:
127.0.0.1:52696
and sometimes like this
[::1]:42120
:
With what it can be connected?
I believe that it is also with the nginx config, because locally everything works as it should.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvelichitel, 2020-08-30
@wargych

user.conn.RemoteAddr().String()
gives the nginx IP with which conn. To transfer the real ip of users, you need to attach it to the request header. Something like nginx config

location / {
...
    proxy_set_header X-Real-IP $remote_addr;
...

And then pull out of this header in the Go handler something like this As for , as already answered, it's just ipv6 notation
*http.Request.Header.Get("X-Real-IP")
[::1]:42120

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question