调整nginx代理时获取真实ip的优先级

pull/45/head
David 2022-03-01 09:57:00 +08:00
parent 3757caf5d5
commit c217c8bd4e
1 changed files with 8 additions and 10 deletions

View File

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