Django获取request中的真实IP

1、如果没有通过nginx的反向代理:request.META.get("REMOTE_ADDR")

2、在nginx配置中增加HTTP_X_FORWARDED_FOR 

 X-Forwarded-For请求头格式:X-Forwarded-For:client, proxy1, proxy2

proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header REMOTE-HOST $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_f
  x = request.META.get("HTTP_X_FORWARD_FOR")
  ip = x.split(',')[0] if x else '-'

  

posted @ 2020-07-09 14:35  青花メ  阅读(805)  评论(0编辑  收藏  举报