Magento 2 – Get Real IP with Varnish, Nginx, and SSL

magento2nginxsslvarnish

I'm having a setup for Magento 2 with Nginx + Varnish + SSL in ubuntu server 18.04. Now with my setup, I only get all logs in Nginx and inside Magento admin coming from same localhost IP which Varnish listens.

I want to change that.

What the best way to do it?
Using Nginx and

ngx_http_realip_module

Or reconfigure Varnish with a GeoIP module I found here:
Varnish GeoIP

For option one will this add in my Nginx conf file work?

server {
    listen 8082;
    set_real_ip_from   127.0.0.1;
    real_ip_header     X-Forwarded-For;
    <Other Server Options>
}

Do I need to change something in Magento

Nginx.conf

file?

Best Answer

to help you debug this, you can add this php file for example, headers.php:

<?php
foreach($_SERVER as $key_name => $key_value) {
print $key_name . "  =  <font style='color: rgb(0,0,255);'>" . $key_value . "</font><br/>";
}
?>

where you can see all the headers.

make sure you proxy pass correct header:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;

add remote IP variations and check headers with file above.

Related Topic