web服务器获取用户访问的真实ip地址
(2018-03-27 12:12:06)
标签:
web
获取用户的ip
|
分类:
Linux
|
这个话题关联2个方面:
1.若web服务器之前没有配置反向代理服务器,web服务可以直接获得访问用户IP地址。
2.若web服务器之前设置了反向代理服务器(nginx或者haproxy),没有修改日志格式的话,web服务的日志获取的访问IP为反向代理服务的IP地址。
总结一下各种情况:
一、反向代理服务为nginx,web服务为 Apache
1.反向代理nginx配置:
在location 下面添加
proxy_set_header X-Real-IP $remote_addr;
Apache配置:
在主配置文件中,找到如下:
修改前:
增加了\”%{X-Real-IP}i\”
LogFormat "%l %u %t
\"%r\"%>s %b \"%{Referer}i\"
\”%{X-Real-IP}i\”\"%{User-Agent}i\"" combined
反向代理为nginx,web服务为nginx
2.反向代理nginx配置:
在location 下面添加
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
web服务器nginx配置文件中:添加 "$http_x_forwarded_for"
即可
log_format main
'$http_x_real_ip - $remote_user '
'[$time_local] '$status $body_bytes_sent
"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';
二、反向代理为haproxy,web服务器为nginx或Apache。
反向代理haproxy配置为:
可以在它的配置文件中加上下面两行:
option httpclose
option forwardfor
web服务器nginx配置文件中:
添加 "$http_x_forwarded_for" 即可
log_format
main '$http_x_real_ip - $remote_user [$time_local] $status
$body_bytes_sent
"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';
web服务器Apache配置文件中:
添加 \"%{X-ForWarded-For}i\" 即可
LogFormat "%h %l %u %t \"%r\" %>s %b
\"%{Referer}i\"\"%{X-ForWarded-For}i\" \"%{User-Agent}i\""
combined
喜欢
0
赠金笔
加载中,请稍候......