微信h5 在有代理的情况下,获取客户端的真实ip
2017-11-08 00:00
1221 查看
摘要: 在有代理的情况下,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端WEB服务器得到的数据包的头部源IP地址是代理服务器的IP地址。这样一来,后端服务器的程序就无法获取用户的真实ip。
nginx有代理的情况:
在nginx中配置中加入
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Apache有代理的情况:
vi /usr/local/apache/conf/httpd.conf
Include conf/extra/httpd-remoteip.conf
vi /usr/local/apache/conf/extra/httpd-remoteip.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1
代码 示例
string GetClientIp(CgiInput* poInput)
{
string client_ip = "";
string strClientIPList;
GetHttpHeader("X-Forwarded-For", strClientIPList);
if (strClientIPList.empty())
{
GetHttpHeader("X-Real-IP", strClientIPList);
}
if (!strClientIPList.empty())
{
size_t iPos = strClientIPList.find( "," );
if( iPos != std::string::npos )
{
client_ip = strClientIPList.substr( iPos );
}
else
{
client_ip = strClientIPList;
}
}
if (client_ip.empty())
{
GetHttpHeader("PROXY_FORWARDED_FOR", strClientIPList);
// 做下兼容
if(strClientIPList.empty())
{
client_ip = getRemoteAddr();
}
else
{
size_t iPos = strClientIPList.find( "," );
if( iPos != std::string::npos )
{
client_ip = strClientIPList.substr( iPos );
}
else
{
client_ip = strClientIPList;
}
}
}
if(!MMPayCommFunc::IsIp(client_ip))
client_ip = getRemoteAddr();
return client_ip;
}
没有代理的情况
在商户的前端接入层没有做代理的情况下获取ip的方式比较简单,直接获取'REMOTE_ADDR '即可。有代理的情况
在有代理的情况下,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端WEB服务器得到的数据包的头部源IP地址是代理服务器的IP地址。这样一来,后端服务器的程序就无法获取用户的真实ip。nginx有代理的情况:
在nginx中配置中加入
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Apache有代理的情况:
vi /usr/local/apache/conf/httpd.conf
Include conf/extra/httpd-remoteip.conf
vi /usr/local/apache/conf/extra/httpd-remoteip.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1
代码 示例
string GetClientIp(CgiInput* poInput)
{
string client_ip = "";
string strClientIPList;
GetHttpHeader("X-Forwarded-For", strClientIPList);
if (strClientIPList.empty())
{
GetHttpHeader("X-Real-IP", strClientIPList);
}
if (!strClientIPList.empty())
{
size_t iPos = strClientIPList.find( "," );
if( iPos != std::string::npos )
{
client_ip = strClientIPList.substr( iPos );
}
else
{
client_ip = strClientIPList;
}
}
if (client_ip.empty())
{
GetHttpHeader("PROXY_FORWARDED_FOR", strClientIPList);
// 做下兼容
if(strClientIPList.empty())
{
client_ip = getRemoteAddr();
}
else
{
size_t iPos = strClientIPList.find( "," );
if( iPos != std::string::npos )
{
client_ip = strClientIPList.substr( iPos );
}
else
{
client_ip = strClientIPList;
}
}
}
if(!MMPayCommFunc::IsIp(client_ip))
client_ip = getRemoteAddr();
return client_ip;
}
相关文章推荐
- nginx反向代理后应用程序如何获取客户端真实IP?
- nginx反向代理后应用程序如何获取客户端真实IP?
- nginx反向代理后应用程序如何获取客户端真实IP?
- nginx 代理模式下,获取客户端真实IP
- nginx反向代理后应用程序如何获取客户端真实IP?
- 微信H5支付,获取用户真实ip
- Nginx反向代理获取客户端真实IP
- nginx反向代理后应用程序如何获取客户端真实IP?
- 配置Nginx方向代理后,php获取客户端真实IP
- nginx反向代理后应用程序如何获取客户端真实IP?
- 透过代理获取客户端请求的真实ip
- nginx反向代理后应用程序如何获取客户端真实IP?
- java实现穿透代理获取客户端真实ip
- nginx反向代理后应用程序如何获取客户端真实IP?
- nginx 代理模式下,获取客户端真实IP
- nginx 代理模式下,获取客户端真实IP
- nginx反向代理后应用程序如何获取客户端真实IP?
- php获取客户端真实IP 防止代理和作弊
- Nginx多级代理,获取客户端真实请求IP以及每级代理IP
- nginx设置反向代理,获取真实客户端ip