代码之家  ›  专栏  ›  技术社区  ›  Nixarn

未使用nginx和tornado将远程地址发送到Django

  •  10
  • Nixarn  · 技术社区  · 16 年前

    因此,我使用nginx进行了一个简单的设置,用于静态媒体和负载平衡,并使用tornado作为django的web服务器(运行4台服务器)。我的问题是remote_addr没有传递给django,因此我得到一个KeyError:

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

        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是在元密钥前加的,所以我不能只做代理设置头remote\u addr$remote\u addr。如果找不到远程地址密钥,我可以读取X-Real-IP,但我很好奇是否有更智能的解决方案。

    谢谢

    6 回复  |  直到 16 年前
        1
  •  17
  •   Nixarn    16 年前

    下面是我如何解决这个问题的。通过使用此中间件:

    class SetRemoteAddrMiddleware(object):
        def process_request(self, request):
            if not request.META.has_key('REMOTE_ADDR'):
                try:
                    request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP']
                except:
                    request.META['REMOTE_ADDR'] = '1.1.1.1' # This will place a valid IP in REMOTE_ADDR but this shouldn't happen
    

    希望有帮助!

        2
  •  14
  •   Andy Aquino    12 年前

    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 它应该能很好地工作。

    • Django 1.5.4
    • Nginx 1.4.3
    • 龙卷风2.2.1
        3
  •  5
  •   asciitaxi    16 年前

    我有一个类似的设置。在将nginx放在apache前面之后,我注意到apache日志中的IP总是127.0.0.1。安装“libapache2 mod rpaf”似乎解决了这个问题。我不知道你的问题是否与此有关。

        4
  •  5
  •   Community Mohan Dere    9 年前

    在nginx.conf文件中添加“fastcgi_param REMOTE_ADDR$REMOTE_ADDR;”:

        location / {
        # host and port to fastcgi server
        fastcgi_pass 127.0.0.1:8801;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
        ...
        # Add this line!
        fastcgi_param REMOTE_ADDR $remote_addr;
        ...
    }
    

    how to nginx virtual servers + fcgi for django?

        5
  •  2
  •   windyjonas    14 年前

    不,是 not possible 传递远程地址。因此,我知道的唯一解决方案是使用X-Real-IP或X-Forwarded-For,并确保后端正确处理这些问题。

    编辑:这适用于fastcgi_通行证,而不是常规nginx代理_通行证

        6
  •  2
  •   Matthew Schinckel    14 年前

    对我来说,使用以下方法是有效的:

    server {
        listen 80;
        server_name foo.bar.com;
        location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
    

    这适用于django 1.4(特别是localshop)。