Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Description
===========

**ngx_http_geoip2_module** - creates variables with values from the maxmind geoip2 databases based on the client IP (default) or from a specific variable (supports both IPv4 and IPv6)
**ngx_http_geoip2_module** - creates variables with values from the MaxMind GeoIP2 databases based on the client IP (default) or from a specific variable (supports both IPv4 and IPv6)

The module now supports nginx streams and can be used in the same way the http module can be used.

Expand Down Expand Up @@ -45,7 +45,7 @@ OR


## Download Maxmind GeoLite2 Database (optional)
The free GeoLite2 databases are available from [Maxminds website](http://dev.maxmind.com/geoip/geoip2/geolite2/) (requires signing up)
The free GeoLite2 databases are available from [MaxMind's website](http://dev.maxmind.com/geoip/geoip2/geolite2/) (requires signing up)

## Example Usage:
```
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source/
29 changes: 29 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM ubuntu:noble

COPY . /test
COPY source /source

ARG NGINX_VERSION="1.28.0"
ARG GEOIPUPDATE_ACCOUNT_ID
ARG GEOIPUPDATE_EDITION_IDS="GeoLite2-Country GeoLite2-City"
ARG GEOIPUPDATE_LICENSE_KEY_FILE="/build/source/lk.txt"

# build nginx and geoip2 module
WORKDIR /build
RUN apt-get update && apt-get install -y libterm-readline-gnu-perl python3-launchpadlib software-properties-common \
&& add-apt-repository -y ppa:maxmind/ppa && apt-get update \
&& apt-get install -y build-essential curl libmaxminddb0 libmaxminddb-dev libpcre2-dev mmdb-bin zlib1g-dev \
&& curl -sS -4 http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar -xvzf - \
&& cd nginx-${NGINX_VERSION} \
&& cp -v ./conf/fastcgi_params /test/ \
&& ./configure --with-compat --add-dynamic-module=/source \
&& make build modules install

# download maxmind geoip databases
RUN --mount=type=secret,id=GEOIPUPDATE_LICENSE_KEY,target=$GEOIPUPDATE_LICENSE_KEY_FILE \
apt-get install -y fcgiwrap geoipupdate recode \
&& rm -v /etc/GeoIP.conf \
&& geoipupdate -v -d /usr/share/GeoIP

# entrypoint runs fcgiwrapper and nginx
ENTRYPOINT /test/entrypoint
22 changes: 22 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
LOCAL_PORT ?= 8888
PACKAGE_NAME ?= ngx_http_geoip2_module
CONTAINER_NAME ?= $(PACKAGE_NAME)-build

all: clean container run

clean:
docker rmi -f $(CONTAINER_NAME)
docker rm -f $(CONTAINER_NAME)

container: source
docker buildx build --build-arg GEOIPUPDATE_ACCOUNT_ID --build-arg GEOIPUPDATE_EDITION_IDS --secret=type=env,id=GEOIPUPDATE_LICENSE_KEY,src=GEOIPUPDATE_LICENSE_KEY --network=host -t "$(CONTAINER_NAME)" .
docker create --name "$(CONTAINER_NAME)" "$(CONTAINER_NAME)"

run:
docker run -p "$(LOCAL_PORT):80" -it "$(CONTAINER_NAME)"

source:
mkdir -p ./source
find .. -maxdepth 1 -type f -exec cp -v {} source \;

.PHONY: clean container run source
25 changes: 25 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ngx_http_geoip2_module Test Container

This directory contains resources to build a Docker container to test ngx_http_geoip2_module.

First set GEOIPUPDATE_ACCOUNT_ID and GEOIPUPDATE_LICENSE_KEY environment variables for authentication with MaxMind to download databases.

For example run a command like the following:
```
export GEOIPUPDATE_ACCOUNT_ID=12345 GEOIPUPDATE_LICENSE_KEY=$(pass show '/auth/maxmind-license-key')
```

Then run `make` or `make container run` in this directory to build and start the container.

The container exposes Nginx on local port 8888. Make requests in the form http://localhost:8888/$IP to test out the geoip2 module. The IP provided as part of the path is passed to the geoip2 module. The default endpoint prints its environment including variables prefixed with GEOIP2 set by the geoip2 module.

For example:

```
$> curl -fsS localhost:8888/$(curl -sS ident.me) | grep GEOIP2
GEOIP2_CITY_EPOCH=1745917200
GEOIP2_CITY_NAME=Haverhill
GEOIP2_COUNTRY_CODE=US
GEOIP2_COUNTRY_EPOCH=1745917530
GEOIP2_COUNTRY_NAME=United States
```
8 changes: 8 additions & 0 deletions test/cgi-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# cgi-env writes an HTML page listing current environment variables
cgi-env() {
local body=$(env -0 | sort -z | xargs -0 printf "%s\n"| recode ascii..html)
printf "Content-type: text/html\\n\\n"
printf "<html>\\n<head><title>cgi-env</title></head>\\n<body>\\n<pre>\\n%s\\n</pre>\\n</body>\\n</html>\\n" "$body"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then cgi-env "$@"; fi
8 changes: 8 additions & 0 deletions test/entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

sock="/var/run/fcgiwrap.socket"
fcgiwrap -s unix:"$sock" -c 2 -f &
sleep 1
chown www-data: "$sock"
/usr/local/nginx/sbin/nginx -c /test/nginx.conf &
wait
57 changes: 57 additions & 0 deletions test/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
load_module modules/ngx_http_geoip2_module.so;

user www-data www-data;

events {
worker_connections 768;
multi_accept on;
}

http {
access_log /test/access.log;
error_log /test/error.log;

map $check $empty {
default 1;
"~^.+$" 0;
}

geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_city_epoch metadata build_epoch;
$geoip2_city_name source=$arg city names en;
}

geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
auto_reload 5m;
$geoip2_country_epoch metadata build_epoch;
$geoip2_country_code source=$arg country iso_code;
$geoip2_country_name source=$arg country names en;
}

server {
listen 80 default_server;
listen [::]:80 default_server;

location ~ /(.+)$ {
gzip off;
set $arg $1;
root /test/;
include fastcgi_params;

set $check "$geoip2_country_code";
if ($empty) {
return 404;
}

fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root/cgi-env;

fastcgi_param GEOIP2_CITY_EPOCH $geoip2_city_epoch;
fastcgi_param GEOIP2_CITY_NAME $geoip2_city_name;
fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_country_code;
fastcgi_param GEOIP2_COUNTRY_EPOCH $geoip2_country_epoch;
fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_country_name;
}
}
}