<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://briansnelson.com/index.php?action=history&amp;feed=atom&amp;title=Magento_2_How_to_Add_Alternative_HTTP_headers</id>
		<title>Magento 2 How to Add Alternative HTTP headers - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://briansnelson.com/index.php?action=history&amp;feed=atom&amp;title=Magento_2_How_to_Add_Alternative_HTTP_headers"/>
		<link rel="alternate" type="text/html" href="https://briansnelson.com/index.php?title=Magento_2_How_to_Add_Alternative_HTTP_headers&amp;action=history"/>
		<updated>2026-06-04T04:05:43Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.24.1</generator>

	<entry>
		<id>https://briansnelson.com/index.php?title=Magento_2_How_to_Add_Alternative_HTTP_headers&amp;diff=983&amp;oldid=prev</id>
		<title>Brian: Created page with &quot;==Magento 2 How to Add Alternative HTTP headers==  If you have more than one frontend server running on your Magento 2 store, it’s needed to load balance the traffic between...&quot;</title>
		<link rel="alternate" type="text/html" href="https://briansnelson.com/index.php?title=Magento_2_How_to_Add_Alternative_HTTP_headers&amp;diff=983&amp;oldid=prev"/>
				<updated>2019-06-19T00:57:44Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Magento 2 How to Add Alternative HTTP headers==  If you have more than one frontend server running on your Magento 2 store, it’s needed to load balance the traffic between...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Magento 2 How to Add Alternative HTTP headers==&lt;br /&gt;
&lt;br /&gt;
If you have more than one frontend server running on your Magento 2 store, it’s needed to load balance the traffic between the nodes. In this case we have a new instance between the browser and the web-server.&lt;br /&gt;
&lt;br /&gt;
Often it’s a system like [[Main_Page#HAProxy|HAProxy]] or [[Main_Page#Varnish_Cache|Varnish]].&lt;br /&gt;
&lt;br /&gt;
If the load balancer or proxy receives a request from a browser, it forwards it to backend server in the internal network. &lt;br /&gt;
&lt;br /&gt;
The IP address of the client is than added to a forward header which contains the IP address of a forward chain.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
X-Forwarded-For: client, proxy1, proxy2&lt;br /&gt;
&lt;br /&gt;
In some situation i.e. for GEO-IP checks, you need the real IP address of a client. &lt;br /&gt;
&lt;br /&gt;
If you don’t configure Magento the remote address is always the proxy or load balancer (127.0.0.1).&lt;br /&gt;
&lt;br /&gt;
That’s not what we want. We need the first part of the comma separated list of the IP chain. &lt;br /&gt;
&lt;br /&gt;
Magento offers us a mechanism to solve this issue.&lt;br /&gt;
&lt;br /&gt;
===Remote Address===&lt;br /&gt;
&lt;br /&gt;
The key to solve the issue is the class &lt;br /&gt;
&lt;br /&gt;
 Magento\Framework\HTTP\PhpEnvironment\RemoteAddress &lt;br /&gt;
&lt;br /&gt;
which is provided by the Magento 2 framework.&lt;br /&gt;
&lt;br /&gt;
In Magento 2 it’s prohibited to call the $_SERVER['REMOTE_ADD'] directly. &lt;br /&gt;
&lt;br /&gt;
The RemoteAddress class is a wrapper to deal with the remote address.&lt;br /&gt;
&lt;br /&gt;
The correct way to get the remote address is this:&lt;br /&gt;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;&lt;br /&gt;
&lt;br /&gt;
 use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;&lt;br /&gt;
   &lt;br /&gt;
 class MyClass&lt;br /&gt;
 {&lt;br /&gt;
     /**&lt;br /&gt;
      * @var RemoteAddress&lt;br /&gt;
      */&lt;br /&gt;
     private $remoteAddress;&lt;br /&gt;
  &lt;br /&gt;
     public function __construct(RemoteAddress $remoteAddress)&lt;br /&gt;
     {&lt;br /&gt;
         $this-&amp;gt;remoteAddress = $remoteAddress;&lt;br /&gt;
     }&lt;br /&gt;
  &lt;br /&gt;
     public function doSomething()&lt;br /&gt;
     {&lt;br /&gt;
         $ipAddressOfTheClient = $this-&amp;gt;remoteAddress-&amp;gt;getRemoteAddress();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Configuration===&lt;br /&gt;
&lt;br /&gt;
It’s possible to inject a list of alternative headers to the RemoteAddress class by Dependency Injection. &lt;br /&gt;
&lt;br /&gt;
This config isn’t related to any module. It’s a special config for a production server setup.&lt;br /&gt;
&lt;br /&gt;
It’s important to know that Magento 2 will load any di.xml file from any subfolder of the '''app/etc''' folder!&lt;br /&gt;
&lt;br /&gt;
With this information we can create a subfolder like '''app/etc/myproject/di.xml''' with the following content:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt; &lt;br /&gt;
 &amp;lt;config xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;br /&gt;
         xsi:noNamespaceSchemaLocation=&amp;quot;urn:magento:framework:ObjectManager/etc/config.xsd&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
     &amp;lt;type name=&amp;quot;Magento\Framework\HTTP\PhpEnvironment\RemoteAddress&amp;quot;&amp;gt;&lt;br /&gt;
         &amp;lt;arguments&amp;gt;&lt;br /&gt;
             &amp;lt;argument name=&amp;quot;alternativeHeaders&amp;quot; xsi:type=&amp;quot;array&amp;quot;&amp;gt;&lt;br /&gt;
                 &amp;lt;item name=&amp;quot;x-forwarded-for&amp;quot; xsi:type=&amp;quot;string&amp;quot;&amp;gt;HTTP_X_FORWARDED_FOR&amp;lt;/item&amp;gt;&lt;br /&gt;
             &amp;lt;/argument&amp;gt;&lt;br /&gt;
         &amp;lt;/arguments&amp;gt;&lt;br /&gt;
     &amp;lt;/type&amp;gt;&lt;br /&gt;
 &amp;lt;/config&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that Magento will look into the given HTTP header „X-Forwarded-For“. The name of the header is normalized by PHP.&lt;br /&gt;
&lt;br /&gt;
'''X-Forwarded-For is available as $_SERVER['HTTP_XFORWARDED_FOR'].'''&lt;br /&gt;
&lt;br /&gt;
It’s possible to add more than one header to alternative header list.&lt;/div&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>