1

I'm using Nginx reverse proxy to apache2 using following tutorial.

Then i try to install geoip to Nginx using this tutorial

The reverse proxy works flawlessly for a while now, until I tried to install the geoip database so I can get the country code on PHP.

I have the following on nginx configured as what the tutorial instructed.

location ~ \.php$ {

    #       location / {
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            #include snippets/fastcgi-php.conf;

           proxy_pass http://1.2.3.4:8080$request_uri;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
           proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
           proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;

           proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
           proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
           proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
           proxy_set_header GEOIP_REGION $geoip_region;
           proxy_set_header GEOIP_CITY $geoip_city;
           proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
           proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
           proxy_set_header GEOIP_LATITUDE $geoip_latitude;
           proxy_set_header GEOIP_LONGITUDE $geoip_longitude;
    include /etc/nginx/proxy_params;
    }

If i use proxy_pass to apache2, the GEOIP variables are **all not shown in phpinfo.

If I use nginx fastcgi_pass directly (turning off reverse proxy to apache) i can get the environment variables set and they are reflected in the phpinfo.

It appears that proxy_set_header may not be working as apache doesn't seems to be reading it.

What do I have to do so apache can get all the variables?

Someone Special
  • 171
  • 2
  • 8

1 Answers1

2

It appears that this tutorial and multiple other websites which copy the codes from this geoip nginx tutorial is wrong/outdated.

when setting the proxy header, it seems that you cannot use underscore(_) and muse use (-). So after changing from

proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;

to this

proxy_set_header GEOIP-CITY-COUNTRY-CODE $geoip_city_country_code;

apache is able to get the variables and it's also showing on my phpinfo now.

My complete location block for php hence looks like this now.

location ~ \.php$ {

           proxy_pass http://1.2.3.4:8080$request_uri;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header GEOIP-COUNTRY-CODE $geoip_country_code;
           proxy_set_header GEOIP-COUNTRY-CODE3 $geoip_country_code3;
           proxy_set_header GEOIP-COUNTRY-NAME $geoip_country_name;

           proxy_set_header GEOIP-CITY-COUNTRY-CODE $geoip_city_country_code;
           proxy_set_header GEOIP-CITY-COUNTRY-CODE3 $geoip_city_country_code3;
           proxy_set_header GEOIP-CITY-COUNTRY-NAME $geoip_city_country_name;
           proxy_set_header GEOIP-REGION $geoip_region;
           proxy_set_header GEOIP-CITY $geoip_city;
           proxy_set_header GEOIP-POSTAL_CODE $geoip_postal_code;
           proxy_set_header GEOIP-CITY-CONTINENT-CODE $geoip_city_continent_code;
           proxy_set_header GEOIP-LATITUDE $geoip_latitude;
           proxy_set_header GEOIP-LONGITUDE $geoip_longitude;
    include /etc/nginx/proxy_params;
    }

With he above settings I would be able to get the following server variables with their values populated. Do note that some servers added the HTTP prefix for security reasons. And somehow... the dashes get converted back to underscores again.

$_SERVER['HTTP_GEOIP_LONGITUDE'] 
$_SERVER['HTTP_GEOIP_LATITUDE'] 
$_SERVER['HTTP_GEOIP_CITY'] 
$_SERVER['HTTP_GEOIP_REGION'] 
$_SERVER['HTTP_GEOIP_CITY_COUNTRY_CODE'] 
$_SERVER['HTTP_GEOIP_COUNTRY_NAME'] 
$_SERVER['HTTP_GEOIP_COUNTRY_CODE3'] 
$_SERVER['HTTP_GEOIP_COUNTRY_CODE']
Someone Special
  • 171
  • 2
  • 8