REMOTE_ADDR没有被发送到Django使用nginx&龙卷风 [英] REMOTE_ADDR not getting sent to Django using nginx & tornado

查看:294
本文介绍了REMOTE_ADDR没有被发送到Django使用nginx&龙卷风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的设置与nginx静态媒体和负载平衡和龙卷风作为web服务器django(4服务器运行)。我的问题是remote_addr没有被传递给django,所以我得到一个KeyError:

So I got a simple setup with nginx for static media and load balancing and tornado as webserver for django (4 servers running). My problem is remote_addr not getting passed on to django so I'm getting a KeyError:

article.ip = request.META ['REMOTE_ADDR' ]

由于nginx.conf,远程地址通过X-Real-IP(HTTP_X_REAL_IP)发送:

The remote address is getting sent through as X-Real-IP (HTTP_X_REAL_IP) thanks to the nginx.conf:

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect false;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://frontends;
    }

由于HTTP被添加到META密钥中,我不能只做proxy_set_header remote_addr $ REMOTE_ADDR。我可以做的是阅读X-Real-IP,如果没有找到远程addr密钥,但如果有一个更聪明的解决方案,我很好奇。

As HTTP is prepended to the META key I can't just do proxy_set_header remote_addr $remote_addr. What I could do is read the X-Real-IP if no remote addr key is found but I'm curious if there's a smarter solution.

谢谢! >

Thanks!

推荐答案

尝试这一个:

location / {
    proxy_pass http://frontends;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header REMOTE_ADDR $remote_addr;
}

只需添加 proxy_set_header REMOTE_ADDR 它应该是很好的。

Just add proxy_set_header REMOTE_ADDR and it should be work well.

尝试:


  • Django 1.5 .4

  • Nginx 1.4.3

  • 龙卷风2.2.1

这篇关于REMOTE_ADDR没有被发送到Django使用nginx&龙卷风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆