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

相关推荐
Csvn8 小时前
Nginx 与 OpenResty:阶段钩子、shared dict 与动态 upstream/证书
后端·nginx
李纲明2 天前
WordPress 标签页或附件页被大量收录:先核站点地图与 noindex,再查归档
wordpress·标签页·devdiy·站点地图·noindex·附件页·索引控制
Csvn2 天前
Nginx HTTPS 与安全加固:会话复用、OCSP、mTLS 与限流取舍
后端·nginx
沫璃染墨2 天前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组
运维·服务器·网络·网络协议·tcp/ip·ip
jaysee-sjc2 天前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
xbzb3 天前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
Csvn3 天前
Nginx 日志、监控与排障:全字段日志、条件日志与证据链定位
后端·nginx
李纲明3 天前
WordPress 广告落地页与站内不一致或支付物流说明乱:先核对齐信息,再查模板
wordpress·devdiy·广告落地页·物流说明·支付说明·信息对齐·页面模板
李纲明3 天前
WordPress 帮助文档放在另一系统几乎搜不到:先核子目录与反向代理,再查支柱页
wordpress·反向代理·内容中心·帮助文档·devdiy·子目录·支柱页
杨凯凡3 天前
Spring Boot SSE 上线后不实时、频繁断线?Nginx 缓冲、心跳与连接回收实战
spring boot·后端·nginx