Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It return unexpected remote IP when nginx proxy pass to "localhost" address. #2966

Closed
beanbest opened this issue Nov 28, 2021 · 3 comments
Closed

Comments

@beanbest
Copy link

beanbest commented Nov 28, 2021

I create my app and serve it on a nginx proxy config.

my gin app:

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/ip", func(c *gin.Context) {
		c.String(http.StatusOK, c.ClientIP())
	})
	r.Run(":9000")
}

my nginx config:

location / {
        proxy_pass  http://localhost:9000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Appengine-Remote-Addr $remote_addr;
        add_header Access-Control-Allow-Origin *;
        proxy_set_header   Upgrade          $http_upgrade;
        proxy_set_header   Connection       upgrade;
        proxy_set_header   Accept-Encoding  gzip;
    }

my /etc/hosts config

::1	 localhost	localhost.localdomain	localhost6	localhost6.localdomain6
127.0.0.1	localhost	localhost.localdomain	localhost4	localhost4.localdomain4

request result:

curl -s http://test.beanbest.net/ip
10.10.10.123
curl -s http://test.beanbest.net/ip
::1
curl -s http://test.beanbest.net/ip
10.10.10.123
curl -s http://test.beanbest.net/ip
::1
curl -s http://test.beanbest.net/ip
10.10.10.123
curl -s http://test.beanbest.net/ip
::1

I expect that it return 10.10.10.123 everytime,But it return ::1 and 10.10.10.123 alternately.

Environment

  • go version: go1.17.3 linux/amd64
@Bisstocuz
Copy link
Contributor

Bisstocuz commented Nov 29, 2021

There is a temporary solution below, I will create a new PR to fix this.

You should set the bind address to a IPv4 value:

func main() {
	r := gin.Default()
	r.GET("/ip", func(c *gin.Context) {
		c.String(http.StatusOK, c.ClientIP())
	})
	r.Run("127.0.0.1:9000")
}

Or give a IPv4 upstream address in nginx:

location / {
        proxy_pass  http://127.0.0.1:9000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Appengine-Remote-Addr $remote_addr;
        add_header Access-Control-Allow-Origin *;
        proxy_set_header   Upgrade          $http_upgrade;
        proxy_set_header   Connection       upgrade;
        proxy_set_header   Accept-Encoding  gzip;
    }

@beanbest
Copy link
Author

Yes, the above method can be used to temporarily circumvent this problem.

And looking forward, Gin will solve this problem completely.

@Bisstocuz
Copy link
Contributor

Hi, #2967 has been merged, you can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants