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

与nginx配合的情况下ip_limit无法获取原始IP #353

Closed
h-hy opened this issue Jan 5, 2020 · 8 comments
Closed

与nginx配合的情况下ip_limit无法获取原始IP #353

h-hy opened this issue Jan 5, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@h-hy
Copy link

h-hy commented Jan 5, 2020

场景:
在使用 https://cnlh.github.io/nps/#/nps_extend?id=%e4%b8%8enginx%e9%85%8d%e5%90%88
介绍的与nginx配合的情况下,
ip_limit功能获取到的是127.0.0.1,并且没有通过header获取的逻辑。
https://cnlh.github.io/nps/#/feature?id=%e9%99%90%e5%88%b6ip%e8%ae%bf%e9%97%ae

查阅代码发现是取了http.Request.RemoteAddr,即TCP握手的IP。
建议加上获取nginx的header的配置功能。

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@h-hy h-hy added the enhancement New feature or request label Jan 5, 2020
@ffdfgdfg
Copy link
Collaborator

ffdfgdfg commented Jan 6, 2020

我咋觉得这个需求不太成立呢,限制IP不应该通过HTTP的header来吧,这样的话就会存在一个问题是假如header进行伪造,那么不管哪里的IP都能访问了啊,所以单一的加入感觉会引发更多的问题呢
况且,主要目的也不是为了HTTP吧

@h-hy
Copy link
Author

h-hy commented Jan 6, 2020

我咋觉得这个需求不太成立呢,限制IP不应该通过HTTP的header来吧,这样的话就会存在一个问题是假如header进行伪造,那么不管哪里的IP都能访问了啊,所以单一的加入感觉会引发更多的问题呢
况且,主要目的也不是为了HTTP吧

这个场景的前提是:
在使用 https://cnlh.github.io/nps/#/nps_extend?id=%e4%b8%8enginx%e9%85%8d%e5%90%88
介绍的与nginx配合的情况下。

这种场景下拿到的RemoteAddr是127.0.0.1。就和七层负载均衡转发一样的道理。

@ffdfgdfg
Copy link
Collaborator

ffdfgdfg commented Jan 6, 2020

我怎么觉得,这里nps应该限制的是反代服务器的IP,仅限反代服务器才能访问啊

@h-hy
Copy link
Author

h-hy commented Jan 6, 2020

我怎么觉得,这里nps应该限制的是反代服务器的IP,仅限反代服务器才能访问啊

嗯,的确,这种场景下,nps必须限制只有反向代理服务器的IP才能访问,否则有可能才出现伪造header。
然后第二步才是其实nps也应该限制原始的访问来源(也就是ip_limit)功能。
= = 不知道你能不能理解我的意思 = =

@ffdfgdfg
Copy link
Collaborator

ffdfgdfg commented Jan 7, 2020

这里就不像Nginx那么灵活使用变量来判断限制了,至少得加个ip来源的选项,只通过remote或者remote一个范围同时header一个范围,感觉有点杂,我们考虑考虑

@h-hy
Copy link
Author

h-hy commented Jan 7, 2020

这里就不像Nginx那么灵活使用变量来判断限制了,至少得加个ip来源的选项,只通过remote或者remote一个范围同时header一个范围,感觉有点杂,我们考虑考虑

https://cnlh.github.io/nps/#/description?id=%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7%e7%9c%9f%e5%ae%9eip
“在域名代理模式中,可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。”

如上,在nps代理到后端的时候,nps是已经会添加nps取到的原始地址,放到header头给业务系统。
这两个header头是在七层负载均衡系统中比较常见的用来传递原始IP的方式(当然前提是要确保请求来着上一层方向代理服务器,防止篡改)。
所以还是可以考虑nps取原始IP的时候,也支持使用可信的反向代理服务器传递的header头。

@ffdfgdfg
Copy link
Collaborator

ffdfgdfg commented Jan 8, 2020

@h-hy 我们计划到0.26的下个功能性版本加入
当然欢迎PR

@h-hy
Copy link
Author

h-hy commented Jan 31, 2020

em... 看了代码变更,怎么感觉不太对……

kiririx pushed a commit to kiririx/nps that referenced this issue Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants