有时,您可以在 Magento 2 管理面板中看到 127.0.0.1 或其他错误的客户 IP,而不是真实的客户 IP 地址。这可能是由于您的 Magento 2 商店启用了代理,例如Varnish、Cloudflare、Sucuri.net  。

要解决错误的 IP 地址问题,您需要:

1.M2目录下新建一个文件app/etc/XIP/di.xml

2.粘贴代码

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config. xsd">

     <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">

         <arguments>

            <argument name="alternativeHeaders" xsi:type="array">

                <item name="x-client-ip" xsi: type="string">HTTP_X_REAL_IP</item>

                <item name="x-cf-connecting-ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>

                <!-- <item name="x-cf- mfcustom-ip" xsi:type="string">SOME_CUSTOM_SERVER_KEY </item> -->

            </argument>

        </arguments>

     </type>

</config>

 

3. 保存文件,运行依赖注入编译,使用 CLI 命令:

php bin/magento setup:di:compile

4.4. 如果这没有帮助,那么您需要使用其他值而不是“ SOME_CUSTOM_SERVER_KEY ”并取消注释行。要获得正确的值,您需要使用代码在 Magento 文件夹中创建 test.php 文件:

<?php

    echo '<pre>';

    var_dump($_SERVER);