WordPress+Nginx+proxy取得真实IP

系统 1882 0

如果 WordPress 运行在 Nginx 作为proxy代理的后端,那么,WP会默认取得$_SERVER['remote_addr']的IP地址.其实这个IP地址是前端Nginx的IP地址,是不对的.如何才能使WordPress取得真实IP地址呢?方法很简单,有几种,下面说下两种:

首先,我们定义一下,假设运行Nginx Proxy的,为服务器A,而运行WordPress的,为服务器B(可以是Nginx, Apache ,Lighttpd,IIS等).

1.使用HttpRealIpModule模块:HTTP_X_REAL_IP.
HttpRealIpModule模块不会默认安装到Nginx中.您需要配置Nginx,添加--with-http_realip_module选项重新编译安装一次Nginx.

然后在Nginx Proxy前端添加:
proxy_set_header X-Real-IP $remote_addr;

重新加载Nginx的配置:
/usr/local/nginx/sbin/nginx -s reload

这时候,客户真实IP会保存在$_SERVER['HTTP_X_REAL_IP']变量中,如果使用 php 程序,输出:

echo $_SERVER['HTTP_X_REAL_IP'];

就会得到用户的真实IP了.

修改WordPress根目录下的wp-config.php:
在第二行添加:
if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}

保存退出,完成!

2.使用HTTP_X_FORWARDED_FOR变量

不用安装其它模块.

然后在Nginx Proxy前端,也就是服务器A添加:
proxy_set_header X-Forwarded-For $remote_addr;

重新加载Nginx的配置:
/usr/local/nginx/sbin/nginx -s reload

这时候,客户真实IP则会保存在$_SERVER['HTTP_X_FORWARDED_FOR']变量中,

修改WordPress根目录下的wp-config.php:
在第二行添加:

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $ips[0];
}

保存退出,完成!

现在试在WP中添加一个新评论,看下是不是使用了真实IP了?

不但在WordPress可以使用这种方法,举一反三,这种方法可以使用到其它相似的环境中.

如果在php程序中这样输出:

<?php

print_r($_SERVER);

?>

将会得到一些类似的信息,如下图:

图中,
REMOTE_ADDR是前端代理Nginx,也就是服务器A的IP地址.
HTTP_X_REAL_IP就是用户的真实IP地址,这个是有用的.
HTTP_X_FORWARDED_FOR也是用户的真实IP地址,这个是有用的.真如果经过很多次跳转的话,这里将会得到一串以","分隔的IP地址列表.

相关阅读:

  1. Nginx+Memcached高速优化DedeCMS之程序修改
  2. Nginx-JSP-Tomcat-PHP
  3. Nginx整合Tomcat
  4. Nginx服务管理脚本
  5. WordPress 自动关键字(词)外链BlogMechanics KeywordLink,SEO优化好插件

Stackflow回复:

Whatever you do, make sure not to trust data sent from the client. $_SERVER['REMOTE_ADDR'] contains the real IP address of the connecting party. That is the most reliable value you can find.

However, they can be behind a proxy server in which case the proxy may have set the $_SERVER['HTTP_X_FORWARDED_FOR'] , but this value is easily spoofed. For example, it can be set by someone without a proxy, or the IP can be an internal IP from the LAN behind the proxy.

This means that if you are going to save the $_SERVER['HTTP_X_FORWARDED_FOR'] , make sure you also save the $_SERVER['REMOTE_ADDR'] value. E.g. by saving both values in different fields in your database.


基本的Nginx配置: 转自 张宴
user www www;

worker_processes 10;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#最大文件描述符
worker_rlimit_nofile 51200;

events
{
use epoll;

worker_connections 51200;
}

http
{
include conf/mime.types;
default_type application/octet-stream;

keepalive_timeout 120;

tcp_nodelay on;

upstream www.s135.com {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}

upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}

server
{
listen 80;
server_name www.s135.com;

location / {
proxy_pass http://www.s135.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

log_format www_s135_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/www.log www_s135_com;
}

server
{
listen 80;
server_name blog.s135.com;

location / {
proxy_pass http://blog.s135.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

log_format blog_s135_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/blog.log blog_s135_com;
}
}
此时获取客户端IP方法如下:
function getIP() {
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if( isset($_SERVER['HTTP_X_REAL_IP']) ) {
return $_SERVER['HTTP_X_REAL_IP']);
}else if(isset($_SERVER['REMOTE_ADDR'])) {
return$_SERVER[' REMOTE_ADDR ']);
} else {
return '';
}
}

WordPress+Nginx+proxy取得真实IP


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论