Ubuntu 版本为 18.04.3 LTS

今天想使用 Nginxnginx-module-geoip 模块实现对不同地域访客的重定向功能,就想起了 Mashiro 的这篇文章:Nginx + GeoIP 区分用户 IP 归属国,打算按照文章的步骤做。

然而在文章的第一步就遇到了问题:

root@castle:~# apt-get install nginx-module-geoip
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package nginx-module-geoip

在网上搜索了一下,有人说需要添加 Nginx PPA 仓库:

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update

照做了以后还是出现错误。无奈卡在第一步只能另寻他路,决定直接从源码开始重新编译。

我的 Nginx 是使用 Oneinstack 一键包编译安装的,通过 nginx -V 命令可以查看编译时的参数:

root@castle:~# nginx -V
nginx version: nginx/1.16.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=openssl-1.1.1c --with-pcre=pcre-8.43 --with-pcre-jit --with-ld-opt=-ljemalloc --with-http_sub_module

接下来我们创建一个新的临时文件夹并且进入:

cd ~ && mkdir nginx-temp && cd nginx-temp

https://nginx.org/en/download.html 下载最新 stable 版本并且解压缩,进入 nginx 源码目录:

wget https://nginx.org/download/nginx-1.16.0.tar.gz
tar -xzf nginx-1.16.0.tar.gz
cd nginx-1.16.0

通过之前 nginx -V 获取的编译参数可以发现,里面需要 OpenSSLpcre 两个库;分别通过 https://www.openssl.org/source/https://ftp.pcre.org/pub/pcre/ 查看两个库的最新版本并且下载,解压缩:

wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz
tar -xzf openssl-1.1.1c.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -xzf pcre-8.43.tar.gz

上述两个库的最新版本和 nginx -V 里面的编译参数符合,故此不需要修改;如果未来发布了新的版本,则需要在编译参数里面修改相应变量到准确的路径,否则会出现类似下述的错误:

/bin/sh: 1: cd: can't cd to ../pcre-8.43
objs/Makefile:1374: recipe for target '../pcre-8.43/Makefile' failed
make[1]: *** [../pcre-8.43/Makefile] Error 2
make[1]: Leaving directory '/root/custom-nginx/nginx-1.16.0'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2

然后开始编译过程:

# 在参数的最后加上 --with-http_geoip_module
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=openssl-1.1.1c --with-pcre=pcre-8.43 --with-pcre-jit --with-ld-opt=-ljemalloc --with-http_sub_module --with-http_geoip_module

这个时候发现出现错误:

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

继续搜索了一下,在 https://serverfault.com/questions/503909/geoip-for-configuration-into-nginx 找到了可行的方案:

apt install libgeoip-dev

然后就可以看到 configure 成功的信息了:

Configuration summary
+ using PCRE library: ../pcre-8.43
+ using OpenSSL library: ../openssl-1.1.1c
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

然后执行 make && make install 命令,如果上述部分没有出错的话,就可以成功编译。

test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/root/custom-nginx/nginx-1.16.0'

通过重启 nginx 看看编译是否成功:

service nginx restart

然后访问了一下托管在这台服务器上的网站,嗯,没挂,成功了。

此时再通过 nginx -V 命令就可以看到新的 mod 已经被添加上去了。

root@castle:~# nginx -V
nginx version: nginx/1.16.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=openssl-1.1.1c --with-pcre=pcre-8.43 --with-pcre-jit --with-ld-opt=-ljemalloc --with-http_sub_module --with-http_geoip_module