REMOTE_ADDR 未使用 nginx & 发送到 Django龙卷风

发布于 2024-08-09 04:31:33 字数 665 浏览 11 评论 0原文

因此,我使用 nginx 进行了简单的设置,用于静态媒体和负载平衡,使用龙卷风作为 django 的网络服务器(4 个服务器正在运行)。我的问题是remote_addr没有传递到django,所以我收到一个KeyError:

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

远程地址作为X-Real-IP发送(HTTP_X_REAL_IP) 感谢 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,但我很好奇是否有更智能的解决方案。

谢谢!

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']

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;
    }

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你没皮卡萌 2024-08-16 04:31:34

添加“fastcgi_param REMOTE_ADDR $remote_addr;”到 nginx.conf 文件:

    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;
    ...
}

来源:如何 nginx 虚拟服务器 + fcgi对于 Django?

Add "fastcgi_param REMOTE_ADDR $remote_addr;" to the nginx.conf file:

    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;
    ...
}

Source: how to nginx virtual servers + fcgi for django?

赢得她心 2024-08-16 04:31:34

不,不可能传递remote_addr。因此,我知道的唯一解决方案是使用 X-Real-IP 或 X-Forwarded-For 并确保后端正确处理这些。

编辑:这适用于 fastcgi_pass,而不是常规 nginx proxy_pass

No, it's not possible to pass on remote_addr. So the only solution that I know of is to use X-Real-IP or X-Forwarded-For and make sure that the backend handles these correctly.

Edit: this applies to fastcgi_pass, not regular nginx proxy_pass

鲸落 2024-08-16 04:31:34

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

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)。

For me, using the following worked:

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;
    }
}

This works with django 1.4 (specifically, localshop).

回心转意 2024-08-16 04:31:33

这是我解决问题的方法。通过使用这个中间件:

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

希望有帮助!

Here's how I solved the problem. By using this middleware:

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

Hope that helps!

情丝乱 2024-08-16 04:31:33

试试这个:

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
  • Tornado 2.2.1

Try this one:

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;
}

Just add proxy_set_header REMOTE_ADDR and it should be work well.

Tried with:

  • Django 1.5.4
  • Nginx 1.4.3
  • Tornado 2.2.1
绳情 2024-08-16 04:31:33

我有类似的设置。将nginx放在apache前面后,我注意到apache日志中的IP始终是127.0.0.1。安装“libapache2-mod-rpaf”似乎可以修复它。我不知道你的问题是否相关。

I have a similar setup. After putting nginx in front of apache, I noticed that the IP in the apache logs was always 127.0.0.1. Installing "libapache2-mod-rpaf" seemed to fix it. I have no idea if your problem is related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文