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

相关推荐
酷爱码4 小时前
Nginx 配置 HTTPS 与 WSS 完整指南(最新推荐)
nginx·https·iphone
mqiqe4 小时前
Nginx 代理Https服务
nginx·https·iphone
Alex_81D8 小时前
Https流式输出一次输出一大段,一卡一卡的-解决方案
nginx·http·https
π大星星️12 小时前
LVS + Keepalived + Nginx 高可用负载均衡系统实验
nginx·负载均衡·lvs
一个小白55512 小时前
nginx 的反向代理 负载均衡 动静分离 重写
linux·运维·前端·nginx·centos·firefox
果子⌂12 小时前
Nginx核心服务
运维·nginx
2301_8032977514 小时前
nginx网站服务
linux·运维·nginx
L汐1 天前
05 部署Nginx反向代理
运维·nginx·github
曼汐 .1 天前
企业网站架构部署与优化-Nginx核心功能
前端·nginx·架构
zy happy1 天前
黑马点评前端Nginx启动失败问题解决记录
java·运维·前端·spring boot·nginx·spring