Sometimes, instead of the real customer IP address, you can see 127.0.0.1 or some other wrong customer IP in your Magento 2 Admin panel. It can be due to proxies, like Varnish, Cloudflare, Sucuri.net, that may be enabled for your Magento 2 store.

In this guide, we'll explore how to resolve this issue.

Note: If you use the Magento 2 Geo IP Database Extension by Magefan in version >2.3.5 and still encounter the wrong IP address issue, contact us for further investigation.

To solve the wrong IP address issue, you need to:

1. Create a new file in the Magento 2 directory:

app/etc/XIP/di.xml

2. Paste the code:

<?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-cf-connecting-ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>
<item name="x-client-ip" xsi:type="string">HTTP_X_REAL_IP</item>
</argument>
</arguments>
</type>
</config>

3. Save the file, and run dependency injection compilation, using the CLI command:

php bin/magento setup:di:compile

4. If this does not help, remove the previously added code from the file and add the following:

<?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-cf-mfcustom-ip" xsi:type="string">SOME_CUSTOM_SERVER_KEY</item>
</argument>
</arguments>
</type>
</config>

You need to use the correct value instead of "SOME_CUSTOM_SERVER_KEY". To get it, create a test.php file in your Magento folder with the following code:

<?php
echo '<pre>';
var_dump($_SERVER);

and execute this script in a browser (URL: https://mydomain.com/test.php). The purpose is to define the right key that stores customer IP in your specific case.