在 Apache for Django 中访问 HTTP_X_FORWARDED_FOR 标头
我想在 Django 中读取客户端的 IP 地址。当我现在尝试使用 HTTP_X_FORWARDED_FOR 标头执行此操作时,它失败了。钥匙不存在。
显然这与配置我的 Apache 服务器有关(我正在使用 apache 和 mod_wsgi 进行部署)。我必须将其配置为反向代理吗?我该如何做到这一点,是否存在安全隐患?
谢谢, 布伦丹
I want to read client's IP address in Django. When I try to do so now with the HTTP_X_FORWARDED_FOR Header, it fails. The key is not present.
Apparently this is related to configuring my Apache server (I'm deploying with apache and mod_wsgi). I have to configure it as a reverse proxy? How do I do that, are there security implications?
Thanks,
Brendan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常这些标头可在
request.META
中找到。因此,您可以尝试request.META['HTTP_X_FORWARDED_FOR']
。您是否也使用 Apache 作为反向代理?这对我来说似乎不对。通常,人们使用像 nginx 这样的轻量级静态服务器作为运行应用程序服务器的 Apache 的反向代理。 Nginx 可以使用 proxy_set_header 配置条目发送您喜欢的任何标头。
Usually these headers are available in
request.META
. So you might tryrequest.META['HTTP_X_FORWARDED_FOR']
.Are you using Apache as a reverse proxy as well? This doesn't seem right to me. Usually one uses a lighter weight static server like nginx as the reverse proxy to Apache running the app server. Nginx can send any headers you like using the
proxy_set_header
config entry.我不熟悉 mod_wsgi,但通常客户端 IP 地址可以在 REMOTE_ADDR 环境变量中找到。
如果客户端通过代理访问网站,或者您的设置包括反向代理,则代理地址将位于 REMOTE_ADDR 变量中,并且代理可能会复制 HTTP_X_FORWARDED_FOR 中的原始客户端 IP(取决于其配置)。
如果您有请求对象,则可以像这样访问这些环境变量:
不需要更改 Apache 配置或配置反向代理来获取客户端的 IP 地址。
I'm not familiar with mod_wsgi, but usually the client IP address is available in the REMOTE_ADDR environment variable.
If the client is accessing the website through a proxy, or if your setup includes a reverse proxy, the proxy address will be in the REMOTE_ADDR variable instead, and the proxy may copy the original client IP in HTTP_X_FORWARDED_FOR (depending on it's configuration).
If you have a request object, you can access these environment variables like this :
There should be no need to change your Apache configuration or configure a reverse proxy just to get the client's IP address.