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

相关推荐
果子⌂44 分钟前
Zabbix 企业级高级应用
linux·运维·nginx·zabbix
冬夜戏雪12 小时前
nginx改配置文件还是会访问默认nginx欢迎页面排查 + 域名访问
css·nginx·css3
星辰云-16 小时前
Nginx笔记
运维·笔记·nginx
亲爱的非洲野猪1 天前
Nginx vs Spring Cloud Gateway:限流功能深度对比与实践指南
运维·nginx
石小千1 天前
Nginx服务做负载均衡网关
运维·nginx·负载均衡
叁金Coder2 天前
业务系统跳转Nacos免登录方案实践
前端·javascript·nginx·nacos
云动雨颤2 天前
WordPress安全加速:Cloudflare + Nginx缓存优化方案
服务器·wordpress
William一直在路上3 天前
深度解析:Nginx的卓越性能
运维·nginx
小十十3 天前
K8s+Nginx-ingress+Websocket基础知识理解
websocket·nginx·kubernetes
hzsnone3 天前
公网服务器上Nginx或者Openresty如何屏蔽IP直接扫描
服务器·nginx·openresty