Browsed by
Tag: proxy

How to add alternative HTTP headers to Magento 2?

How to add alternative HTTP headers to Magento 2?

If you have more than one frontend server running in your business, 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.

Often it’s a system like HAProxy or Varnish.

Simple load balancer/proxy setup

If the load balancer or proxy receives a request from a browser, it forwards it to backend server in the internal network. The IP address of the client is than added to a forward header which contains the IP address of a forward chain.


X-Forwarded-For: client, proxy1, proxy2

In some situation i.e. for GEO-IP checks, you need the real IP address of a client. If you don’t configure Magento the remote address is always

That’s not what we want. We need the first part of the comma separated list of the IP chain. Magento offers us a mechanism to solve this issue.

Remote Address

The key to solve the issue is the class Magento\Framework\HTTP\PhpEnvironment\RemoteAddress which is provided by the Magento 2 framework.

In Magento 2 it’s prohibited to call the $_SERVER['REMOTE_ADD'] directly. The RemoteAddress class is a wrapper to deal with the remote address.

The correct way to get the remote address is this:
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;


It’s possible to inject a list of alternative headers to the RemoteAddress class by Dependency Injection. This config isn’t related to any module. It’s a special config for a production server setup.

It’s important to know that Magento 2 will load any „di.xml“ file from any subfolder of the „app/etc“ folder!

With this information we can create a subfolder like „app/etc/myproject/di.xml“ with the following content:


After that Magento will look into the given HTTP header „X-Forwarded-For“. The name of the header is normalized by PHP.
X-Forwarded-For is available as $_SERVER['HTTP_XFORWARDED_FOR'].

It’s possible to add more than one header to alternative header list.

That’s it. Have fun with Magento 2. 🙂

– Creator of n98-magerun
– Fan of football club @wormatia
– Magento user since version 0.8 beta
– 3x certified Magento developer
– PHP Top 1.000 developer (yes, I’m PHP4 certified and sooooo old)
– Chief development officer at netz98
Docker: Simplified container mapping for local development

Docker: Simplified container mapping for local development

When you are working with docker on your local machine, you often have to map your local ports to different container and end up in a port-mapping-mess like this:

  • localhost:80 -> Local apache for native stuff
  • localhost:8080 -> Docker container with apache for testing
  • localhost:8100 -> Some sort of dockered WebApp
  • localhost:59924 -> “Yea, well … don’t know, lets check docker process-list …”

To simplify this mess, we created a little proxy-script (+ environment setup) that will make your life much easier:

The router proxy will add the ability to dispatch your request based on the container name. To archive this, it adds a special TLD which will be used to determine that we want to call an docker container.

In our case (default) this will be .dock, but you can choose a different one if you like.

After configuring the environment (as described within the project description on GitHub) you can simply call your container like this:


this will be proxied to>80/tcp    my_service_container

I’m using this script now for several days and it’s absolutely worth the time for setting up the environment. It’s very easy now to navigate through your services and projects just using your browser history.

I’m very interested what you think about this approach and if you might have found other solutions for this?

So, please leave a comment!