如何将GeoIP HTTP请求标头传递给Apache的后端?

我正在使用nginx将HTTP请求代理到在Docker容器( cbeer / piwik )中的Apache 2.4(mod_php5)范围内运行的PHP应用程序。

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

PHP应用程序是Piwik 2.16。

我使用nginx注入GeoIP HTTP标头:

 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 ""; 

不幸的是Piwik(至less认为它)无法看到通过nginx注入请求的HTTP标头。 在Piwik设置页面上进行地理定位是一个关于Piwik在PHP的$_SERVER找不到GeoIPvariables的警告。 GeoIP头文件到达,例如HTTP_GEOIP_ADDR但必须是GEOIP_ADDR 。 我也不能编辑应用程序来使用这些“新”标题名称。

@RichardSmith提到我必须使用setenv将HTTP_GEOIP_variables映射到Apache内的GEOIP_ 。 我尝试了几个组合,但我没有设法使用variables(请求标头)作为环境variables的值。

 SetEnv GEOIP_ADDR "%{HTTP_GEOIP_ADDR}e" 

这将导致存储在variables中的实际string“ %{HTTP_GEOIP_CITY}e ”而不是HTTP_GEOIP_CITY的值。

如何在Apache中使用setenv将HTTP_GEOIP_variables映射到GEOIP_

你对Apache的SetEnv指令语法的假设是错误的。

从文档来看,似乎是相反的:

SetEnv HTTP_GEOIP_ADDR %{GeoIP-Addr}e

这里是文档的链接:

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

它声明: 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 

所以,如果你交换你的声明,可能会工作。