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加了过滤器执行对应函数里的操作

相关推荐
熊猫钓鱼>_>5 小时前
动态网站发布部署核心问题详解
前端·nginx·容器化·网页开发·云服务器·静态部署
Swift社区7 小时前
Nginx 反向代理配置 React 前端与 Python 后端
前端·nginx·react.js
Dontla8 小时前
Kubernetes流量管理双雄:Ingress与Gateway API解析(Nginx与Ingress与Gateway API的关系)
nginx·kubernetes·gateway
lcx_defender9 小时前
【Docker】Docker部署运行Nginx
nginx·docker·容器
可可嘻嘻大老虎18 小时前
nginx无法访问后端服务问题
运维·nginx
bantinghy1 天前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
Dontla1 天前
Vite代理 vs Nginx代理(开发环境用Vite,生产环境用Nginx)
运维·nginx
No Silver Bullet1 天前
Nginx 内存不足对Web 应用的影响分析
运维·前端·nginx
Access开发易登软件1 天前
Access 窗体中实现数字滚动动画:Timer + Easing 的技术实现
运维·数据库·nginx·microsoft·access
凌寒111 天前
minio预签名访问地址经过Nginx代理后返回403问题
服务器·网络·nginx