V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
python30
V2EX  ›  Python

django 取得发布信息用户的 Ip 总是取得 127.0.0.1 这个, 并不是真实用户的 ip?

  •  
  •   python30 · 2021-11-17 16:05:29 +08:00 · 2459 次点击
    这是一个创建于 862 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class tkinfo(models.Model):
    	author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name = _('author'), on_delete=models.CASCADE)
    	title = models.CharField(max_length=255, verbose_name = u'信息标题')
    	body = models.TextField()
    	excerpt = models.CharField(_(u'话题简介'), default='', max_length=2000, blank=True, null = True)
    	ip_user = models.GenericIPAddressField(unpack_ipv4=True, blank=True, null=True) 
    

    ip_user = models.GenericIPAddressField(unpack_ipv4=True, blank=True, null=True) 用这个以前是可以取得用发布信息用户的真实 ip 地址的

    最近换了服务器。centos 宝塔 搭建的环境 django2.2 + python3.x + nginx +uwsgi

    取得的 ip 地址都是 127.0.0.1 了。 查了半天也没发现哪里有问题? 有知道或遇到过这问题的吗? 多谢

    11 条回复    2021-11-18 17:24:49 +08:00
    hguandl
        1
    hguandl  
       2021-11-17 16:09:48 +08:00
    目测是 nginx 反代时没有设置好 header 。用户的访问通过 nginx 转发到 Django ,IP 当然变成了 localhost 。解决方法是在 nginx 的反代里添加有关来源 IP 的 header ,一般是叫 X-Forwarded-For 或者 X-Real-IP 之类的。网上应该能搜到更具体的教程。
    demo
        2
    demo  
       2021-11-17 16:11:58 +08:00   ❤️ 1
    workg
        3
    workg  
       2021-11-17 16:12:52 +08:00   ❤️ 1
    nginx 反带要带上 request ip header 。你直接访问 django 服务是可以拿到 ip 的
    Vegetable
        4
    Vegetable  
       2021-11-17 16:16:22 +08:00
    这种问题也需要提问吗
    https://cn.bing.com/search?q=django+%E8%8E%B7%E5%8F%96ip+127.0.0.1
    csdn 都安排的明明白白的
    python30
        5
    python30  
    OP
       2021-11-17 16:37:47 +08:00
    @hguandl
    <img src="https://z3.ax1x.com/2021/11/17/I5COK0.png" />
    这是反向代理那里的设置。有什么需要修改的吗?
    python30
        6
    python30  
    OP
       2021-11-17 16:38:41 +08:00
    @demo
    @Vegetable
    谢谢。晚上回去测试一下试试看。
    python30
        7
    python30  
    OP
       2021-11-17 16:40:24 +08:00
    @workg
    我以前直接在 centos 里安装 nginx+uwsgi 环境,是可以直接用上面的代码拿到真实 ip 的,最近换到了宝塔面板上的那个 python 环境里就取不到真实 ip 了。
    hguandl
        8
    hguandl  
       2021-11-17 16:54:06 +08:00
    看起来 nginx 做了设置,那可能就是 django 应用里没有处理好。我不了解这个框架,可以参照前面几楼里的说法,查查 django 的资料看看如何处理这个 proxy header 。
    meiyoumingzi6
        9
    meiyoumingzi6  
       2021-11-17 19:08:37 +08:00
    从 XFF 中获取 ip
    python30
        10
    python30  
    OP
       2021-11-17 20:23:40 +08:00
    @meiyoumingzi6 有演示吗?或者具体说一下? xiexie
    conight
        11
    conight  
       2021-11-18 17:24:49 +08:00
    HTTP_CF_CONNECTING_IP -> HTTP_X_FORWARDED_FOR -> REMOTE_ADDR
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5437 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 08:44 · PVG 16:44 · LAX 01:44 · JFK 04:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.