wordpress评论ip异常问题

在多层nginx中,获取到的ip为最后一个nginx的上层nginxip

具体获取评论ip的代码为

wp-includes/comment.php

复制代码
if ( ! isset( $commentdata['comment_author_IP'] ) ) {
        $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
    }

解决方法1

在wp-config中添加如下代码重写'REMOTE_ADDR'变量

复制代码
// Set the real IP address for clients behind a proxy or load balancer.
function set_real_client_ip() {
    if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        // Check if multiple IPs exist in the header, take the first one.
        $ip_address_list = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
        $real_ip = reset( $ip_address_list );
    } elseif ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        // Fallback to HTTP_CLIENT_IP if available.
        $real_ip = $_SERVER['HTTP_CLIENT_IP'];
    } else {
        // Use REMOTE_ADDR as fallback.
        $real_ip = $_SERVER['REMOTE_ADDR'];
    }

    // Set the REMOTE_ADDR to the real IP address.
    $_SERVER['REMOTE_ADDR'] = $real_ip;
}

// Call the function to set the real IP address.
add_action( 'init', 'set_real_client_ip' );

方法2,安装reverse-proxy-comment-ip-fix插件

插件市场里搜不到,可以上github下载
Tags · WPPlugins/reverse-proxy-comment-ip-fix · GitHub

下载zip包上传到插件安装界面就可以

插件本质上在pre_comment_user_ip加了过滤器执行对应函数里的操作

相关推荐
2301_787328491 小时前
25.负载均衡-Nginx、HAProxy、LVS 全解析
nginx·负载均衡·lvs
siriuuus1 小时前
Nginx 负载均衡调度算法
运维·nginx·负载均衡
WordPress学习笔记1 小时前
wordpress技术外包服务团队
wordpress
tjsoft9 小时前
设置 windows nginx.exe 每天 重启
运维·windows·nginx
舰长11510 小时前
nginx 负载均衡配置
运维·nginx·负载均衡
foundbug9991 天前
查看nginx日志文件
linux·nginx·github
一只程序烽.1 天前
java项目使用宝塔面板部署服务器nginx不能反向代理找到图片资源
java·服务器·nginx
福大大架构师每日一题1 天前
nginx 1.29.2 发布:AWS-LC 支持、QUIC及SSL优化等重要更新
nginx·ssl·aws
金色天际线-1 天前
nginx + spring cloud + redis + mysql + ELFK 部署
redis·nginx·spring cloud
帅帅梓2 天前
nginx访问控制 用户认证 https
运维·nginx·https