陈占占
陈占占
Published on 2022-06-08 / 12 Visits
0
0

Nginx+uWSGI+Django+SSL(https)安全证书中获取访问的IP地址信息

Nginx+uWSGI+Django+SSL(https)安全证书中获取访问的IP地址信息

# SECURE_PROXY_SSL_HEADER SSL安全证书中的
x_forwarded_for = request.META.get("SECURE_PROXY_SSL_HEADER")
    if x_forwarded_for:
        user_ip = x_forwarded_for.split(',')[-1].strip()
    else:
        user_ip = request.META.get('REMOTE_ADDR')

因为网站服务器会使用ngix等代理https(部署了SSL安全证书),如果没有部署SSL安全证书可以使用下面的代码获取http访问的IP地址

if request.META.has_key('HTTP_X_FORWARDED_FOR'):
    user_ip =  request.META['HTTP_X_FORWARDED_FOR']
else:
    user_ip = request.META['REMOTE_ADDR']


Comment