!45 调整get_request_ip优先级

在Nginx代理后端接口时,使用以下配置,无法获取到真实ip,需要调整对应的优先级,以此来获取真实ip。
pull/43/MERGE
dvadmin 2022-03-01 14:34:27 +00:00 committed by Gitee
commit 8bf318ae97
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 8 additions and 10 deletions

View File

@ -40,16 +40,14 @@ def get_request_ip(request):
:param request:
:return:
"""
ip = getattr(request, 'request_ip', None)
if ip:
return ip
ip = request.META.get('REMOTE_ADDR', '')
if not ip:
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', '')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = 'unknown'
return x_forwarded_for.split(',')[-1].strip()
remote_addr = request.META.get('REMOTE_ADDR', '')
if remote_addr:
return remote_addr
ip = getattr(request, 'request_ip', 'unknown')
return ip