如何在Alpine Linux下的nginx中获得原生GeoIP支持

时间:2016-08-11 16:19:30

标签: php nginx geoip alpine

在OS X和Ubuntu下,我通过nginx在PHP中使用GeoIP,如下所示:

nginx.conf:
geoip_country /somepath/GeoIP/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;

php:
$country = $_SERVER["GEOIP_COUNTRY_CODE"]

然而,在Alpine Linux下,nginx(apk add nginx)的软件包版本似乎不包括GeoIP(由nginx -V确认)。

我可以使用--with-http_geoip_module从源代码编译nginx,但是我必须手动更新nginx,这是不可取的。我目前的解决方法如下:

ash:
sudo apk add geoid

php:
$country = exec("geoiplookup $_SERVER[REMOTE_ADDR]");

如何在Alpine Linux下的nginx中获得原生GeoIP支持?

更新:即使我从边缘安装,也无法识别geoip_country

$ apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz
v3.4.4-36-ge528256 [http://dl-cdn.alpinelinux.org/alpine/v3.4/main]
v3.4.0-4239-gff0f660 [http://dl-cdn.alpinelinux.org/alpine/edge/community]
v3.4.0-4267-ga3e3a29 [http://dl-cdn.alpinelinux.org/alpine/edge/main]
OK: 12899 distinct packages available
$ apk add nginx@edge nginx-mod-http-geoip@edge
(1/5) Installing libressl2.4-libcrypto@edge (2.4.3-r1)
(2/5) Installing pcre (8.38-r1)
(3/5) Installing libressl2.4-libssl@edge (2.4.3-r1)
(4/5) Installing nginx@edge (1.10.1-r9)
Executing nginx-1.10.1-r9.pre-install
(5/5) Installing nginx-mod-http-geoip@edge (1.10.1-r9)
Executing busybox-1.24.2-r11.trigger
OK: 500 MiB in 92 packages
$ rc-service nginx restart
 * Caching service dependencies ...                                                                                             [ ok ]
nginx: [emerg] unknown directive "geoip_country" in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed                                                                     [ !! ]
 * ERROR: nginx failed to start

1 个答案:

答案 0 :(得分:2)

我已经更新了nginx包,i.a。添加了对GeoIP作为动态模块的支持。它在包nginx-mod-http-geoip中。

此软件包尚未安装在任何版本中(将在v3.5中),因此您必须使用edge。如果您使用的是稳定版v3.4,则可以使用repository pinning安装边缘的特定软件包:

echo "@edge http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
apk update
apk add nginx@edge nginx-mod-http-geoip@edge

但是,边缘中的nginx是针对较新版本的库而构建的,而不是来自stable分支,因此可能无法正常工作(如果存在某些ABI不兼容的更改)。

在这种情况下,您可以构建nginxyourself或仅使用my repository中的反向移植包:

echo "@geeknet https://alpine.geeknet.cz/packages/v3.4/backports" >> /etc/apk/repositories
wget -O /etc/apk/keys/jakub@jirutka.cz-56d0d9fd.rsa.pub https://raw.githubusercontent.com/jirutka/aports/v3.4/.keys/jakub@jirutka.cz-56d0d9fd.rsa.pub
apk update
apk add nginx@geeknet nginx-mod-http-geoip@geeknet