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

相关推荐
灯火消逝的码头3 小时前
彻底理解 IP 地址,子网掩码,子网划分
ip·子网划分·子网掩码·ip协议·网络号·主机号
BLEACH-heiqiyihu4 小时前
红帽9中nginx-源码编译php
运维·nginx·php
这个需求建议不做5 小时前
vue3打包配置 vite、router、nginx配置
前端·nginx·vue
&星辰入梦来&6 小时前
Nginx从入门到入土(三): 静态资源管理与代理服务
运维·nginx·负载均衡
苹果醋37 小时前
SpringCloud系列之一---搭建高可用的Eureka注册中心
运维·nginx
丶213610 小时前
【Nginx】在 Docker 上安装 Nginx 的详细指南
运维·nginx·docker
ZHOU西口1 天前
微服务实战系列之玩转Docker(十五)
nginx·docker·微服务·云原生·swarm·docker swarm·dockerui
苹果醋31 天前
react 路由 react-router/react-router-dom
运维·nginx
podoor1 天前
wordpress不同网站 调用同一数据表
前端·wordpress
ps酷教程1 天前
nginx进阶篇(二)
nginx