1

I am using nginx to proxy HTTP requests to a PHP application running in the scope of Apache 2.4 (mod_php5) within a Docker container (cbeer/piwik).

                     +------------------------------+
                     |        Docker Container      |
+-------+            |  +--------+          +-----+ |
| nginx |----------->|->| apache |--------->| php | |
+-------+ proxy_pass |  +--------+ mod_php5 +-----+ |
                     +------------------------------+

The PHP application is Piwik 2.16.

I inject GeoIP HTTP headers with nginx:

proxy_set_header GeoIP-Addr             "$remote_addr";
proxy_set_header GeoIP-Country-Code     "$geoip_country_code";
proxy_set_header GeoIP-Country-Name     "$geoip_country_name";
proxy_set_header GeoIP-Continent-Code   "$geoip_city_continent_code";
proxy_set_header GeoIP-Region-Name      "$geoip_region_name";
proxy_set_header GeoIP-Region           "$geoip_region";
proxy_set_header GeoIP-City             "$geoip_city";
proxy_set_header GeoIP-Metro-Code       "$geoip_dma_code";
proxy_set_header GeoIP-Area-Code        "$geoip_area_code";
proxy_set_header GeoIP-Latitude         "$geoip_latitude";
proxy_set_header GeoIP-Longitude        "$geoip_longitude";
proxy_set_header GeoIP-Postal-Code      "$geoip_postal_code";
proxy_set_header GeoIP-Isp              "$geoip_org";
proxy_set_header GeoIP-Organization     "$geoip_org";
proxy_set_header GeoIP-Netspeed         "";

Unfortunately Piwik (at least thinks it) cannot see the via nginx injected request HTTP headers. On the Piwik settings page for geolocationing is a warning about Piwik could not find GeoIP variables within PHP's $_SERVER. The GeoIP headers arrive as e.g. HTTP_GEOIP_ADDR but have to be GEOIP_ADDR. I also cannot edit the application to get use of these "new" header names.

@RichardSmith mentioned that I have to map HTTP_GEOIP_ variables to GEOIP_ within Apache using setenv. I tried several combinations but I did not get managed to use variables (request headers) as values for the environment variable.

SetEnv GEOIP_ADDR "%{HTTP_GEOIP_ADDR}e"

This results into the actual string "%{HTTP_GEOIP_CITY}e" stored in the variable instead of the value of HTTP_GEOIP_CITY.

How to map HTTP_GEOIP_ variables to GEOIP_ within Apache using setenv?

  • I would really not assume, that apache by itself removes any header automatically. Are you sure, that your application is expecting the header in the same format you're providing it, eg. $_SERVER["HTTP_GEOIP_REGION"] By the way, check with phpinfo() to make sure that PHP really can not see the HTTP_GEOIP_* headers. I suspect that this is not the issue. – r_3 Mar 3 '16 at 10:19
  • Maybe you need to map HTTP_GEOIP_ variables to GEOIP_ within Apache using setenv – Richard Smith Mar 3 '16 at 10:26
  • @burnersk should that be a $ rather than a %. I haven't used Apache for a while, so if you get a working solution, post it as an answer. – Richard Smith Mar 3 '16 at 14:14
  • Are you sure that the variables are really set in Nginx .conf? – kaiser Mar 14 '16 at 0:53
  • Yes, otherwise they wouldn't be on on the Apache side with "wrong" header names, right?! – burnersk Mar 14 '16 at 7:11
2

Your assumption on Apache's SetEnv directive syntax is wrong.

From the documentation, it seems it's the other way around:

SetEnv HTTP_GEOIP_ADDR %{GeoIP-Addr}e

Here is the link for the documentation:

https://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv

It states that Sets an internal environment variable, which is then available to Apache HTTP Server modules, and passed on to CGI scripts and SSI pages.

Example

SetEnv SPECIAL_PATH /foo/bin

So, if you swap your declaration, might work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.