How does the GeoIP Redirect module work with Cloudflare?

How does the GeoIP Redirect module work with Cloudflare?

There is a known issue with using the GeoIP Redirect module with Cloudflare. It occurs due to the actual IP address of a customer being replaced by the proxy IP. To introduce compatibility between Cloudflare and the GeoIP Redirect module, follow the steps below.

Step 1. Create a new file in the root Magento directory:

app/etc/XIP/di.xml

Step 2. Paste the following lines of code to the newly created file:

  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4.      <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
  5.          <arguments>
  6.             <argument name="alternativeHeaders" xsi:type="array">
  7.                 <item name="x-client-ip" xsi:type="string">HTTP_X_REAL_IP</item>
  8.                 <item name="x-cf-connecting-ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>
  9.                 <!-- <item name="x-cf-mfcustom-ip" xsi:type="string">SOME_CUSTOM_SERVER_KEY</item> -->
  10.             </argument>
  11.         </arguments>
  12.      </type>
  13. </config>

Step 3. Save the file and run the following CLI command from the root Magento folder:

php bin/magento setup:di:compile

If this doesn't work, then other values should be used instead of SOME_CUSTOM_SERVER_KEY, and the line should be uncommented. To get a proper value, create a test.php file in the root Magento folder and paste this code to it:

  1. <?php
  2. echo '<pre>';
  3. var_dump($_SERVER);

Run this script in the browser; to define the proper key to use, look out for your personal IP address in the right part of the output. Having found your IP address, check the left part of the corresponding line; that would be the key that stores the customer's IP. Copy the text in square brackets and use it instead of the SOME_CUSTOM_SERVER_KEY part in the di.xml file.