如何在Alpine Linux下的nginx中获得本地GeoIP支持?



在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

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


这个包还没有在任何稳定版本中(将在v3.5中),所以你必须使用edge。如果您使用的是稳定版v3.4,则可以使用存储库固定从边缘安装特定的包:

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 in edge是基于比稳定分支新版本的库构建的,所以它可能不能正常工作(如果有一些ABI不兼容的更改)。

在这种情况下,你可以自己构建nginx包,或者直接使用我的存储库中的反向移植包:

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

相关内容

  • 没有找到相关文章

最新更新