• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    C
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MaxMind DB Apache Module

MaxMind DB Apache Module

This module allows you to query MaxMind DB files from Apache 2.2+ using the libmaxminddb library.

Requirements

This module requires Apache 2.2 or 2.4 to be installed, including any corresponding "dev" package, such as apache2-dev on Ubuntu. You should have apxs or apxs2 in your $PATH.

You also must install the libmaxminddb C library.

Installation

From a Named Release Tarball (Recommended)

NOTE: These instructions are for installation from the named .tar.gz tarballs on the Releases page (e.g. mod_maxminddb-*.tar.gz).

To install the module from a tarball, run the following commands from the directory with the extracted source:

./configure
make install

To use another Apache installation, specify a path to the right apxs binary:

./configure --with-apxs=/foo/bar/apxs

From a GitHub "Source Code" Archive / Git Repo Clone (Achtung!)

NOTE: These instructions are for installation from the GitHub "Source Code" archives also available on the Releases page (e.g. X.Y.Z.zip or X.Y.Z.tar.gz), as well as installation directly from a clone of the Git repo. Installation from these sources are possible but will present challenges to users not comfortable with manual dependency resolution.

  1. Ensure the build tools automake, autoconf and libtool are installed.

  2. Extract the archive and switch into the directory containing the extracted source.

  3. Run ./bootstrap. Many users will experience challenges here as there are several dependencies that need to be present before this can complete successfully.

  4. Run:

     ./configure
     make install
    

To use another Apache installation, specify a path to the right apxs binary:

./configure --with-apxs=/foo/bar/apxs

Loading the Module

After installing the module, Apache has to load it. Note the installation does this automatically, so you should not need to do anything. If you're unsure if the module is loaded, ensure there's a LoadModule line somewhere in your config, such as LoadModule maxminddb_module /path/to/mod_maxminddb.so.

Usage

To use this module, you must first download or create a MaxMind DB file. We provide free GeoLite2 databases as well as commercial GeoIP2 databases.

After installing this module and obtaining a database, you must now set up the module in your Apache configuration file (e.g., /etc/apache2/apache2.conf) or in an .htaccess file. You must set MaxMindDBEnable to enable the module, MaxMindDBFile to specify the database to use, and MaxMindDBEnv to bind the desired lookup result to an environment variable. You can also enable MaxMindDBSetNotes if you wish the environment variables to also be set as Apache notes.

This module uses the client IP address for the lookup. This is not always what you want. If you need to use an IP address specified in a header (e.g., by your proxy frontend), mod_remoteip may be used to set the client IP address.

Manually setting the client IP address is also possible. See Client IP address lookup control.

Directives

All directives may appear either in your server configuration or an .htaccess file. Directives in <Location> and <Directory> blocks will also apply to sub-locations and subdirectories. The configuration will be merged with the most specific taking precedence. For instance, a conflicting directive set for a subdirectory will be used for the subdirectory rather than the directive set for the parent location.

Similarly, the main server configuration may set defaults that will be merged into the configuration provided by individual virtual hosts. However, please note that currently no configuration merging is performed between server/vhost and directory configurations.

MaxMindDBEnable

This directive enables or disables the MaxMind DB lookup. Valid settings are On and Off.

MaxMindDBEnable On

MaxMindDBFile

This directive associates a name placeholder with a MaxMind DB file on the disk. You may specify multiple databases, each with its own name.

MaxMindDBFile COUNTRY_DB /usr/local/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBFile CITY_DB    /usr/local/share/GeoIP/GeoLite2-City.mmdb

The name placeholder can be any string that Apache parses as a word. We recommend sticking to letters, numbers, and underscores.

MaxMindDBEnv

This directive assigns the lookup result to an environment variable. The first parameter after the directive is the environment variable. The second parameter is the name of the database followed by the path to the desired data using map keys or 0-based array indexes separated by /.

MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code
MaxMindDBEnv REGION_CODE  CITY_DB/subdivisions/0/iso_code

Keep in mind that the configured environment variable(s) will only be exported if the data lookup succeeds.

MaxMindDBNetworkEnv

This directive assigns the network associated with the IP address to an environment variable. The network will be in CIDR format. This directive may only be used once per database.

MaxMindDBNetworkEnv COUNTRY_DB COUNTRY_NETWORK
MaxMindDBNetworkEnv CITY_DB    CITY_NETWORK

MaxMindDBSetNotes

This directive enables or disables the setting of Apache notes alongside the environment variables set via MaxMindDBEnv. Valid settings are On and Off. It defaults to Off.

MaxMindDBSetNotes On

Exported Environment Variables

In addition to the environment variable specified by MaxMindDBEnv, this module exports MMDB_ADDR, which contains the IP address used for lookups by the module. This is primarily intended for debugging purposes. If MaxMindDBSetNotes is On, all environment variables are also exported as Apache notes.

Client IP address lookup control

In case you want supply your own value for the IP address to lookup, it may be done by setting the environment variable MMDB_ADDR. This can be done, for instance, with ModSecurity in (real) phase 1. Note that mod_setenvif and mod_rewrite cannot be used for this as they are running after this module. For most usages, mod_remoteip is an easier alternative.

Examples

These examples show how to export data from the database into environment variables.

ASN Database

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile ASN_DB /usr/local/share/GeoIP/GeoLite2-ASN.mmdb

    MaxMindDBEnv MM_ASN ASN_DB/autonomous_system_number
    MaxMindDBEnv MM_ASORG ASN_DB/autonomous_system_organization

    MaxMindDBNetworkEnv ASN_DB ASN_DB_NETWORK
</IfModule>

City Database

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile CITY_DB /usr/local/share/GeoIP/GeoLite2-City.mmdb

    MaxMindDBEnv MM_COUNTRY_CODE CITY_DB/country/iso_code
    MaxMindDBEnv MM_COUNTRY_NAME CITY_DB/country/names/en
    MaxMindDBEnv MM_CITY_NAME CITY_DB/city/names/en
    MaxMindDBEnv MM_LONGITUDE CITY_DB/location/longitude
    MaxMindDBEnv MM_LATITUDE CITY_DB/location/latitude

    MaxMindDBNetworkEnv CITY_DB CITY_DB_NETWORK
</IfModule>

Connection-Type Database

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile CONNECTION_TYPE_DB /usr/local/share/GeoIP/GeoIP2-Connection-Type.mmdb

    MaxMindDBEnv MM_CONNECTION_TYPE CONNECTION_TYPE_DB/connection_type

    MaxMindDBNetworkEnv CONNECTION_TYPE_DB CONNECTION_TYPE_DB_NETWORK
</IfModule>

Domain Database

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile DOMAIN_DB /usr/local/share/GeoIP/GeoIP2-Domain.mmdb

    MaxMindDBEnv MM_DOMAIN DOMAIN_DB/domain

    MaxMindDBNetworkEnv DOMAIN_DB DOMAIN_DB_NETWORK
</IfModule>

ISP Database

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile ISP_DB /usr/local/share/GeoIP/GeoIP2-ISP.mmdb

    MaxMindDBEnv MM_ASN ISP_DB/autonomous_system_number
    MaxMindDBEnv MM_ASORG ISP_DB/autonomous_system_organization
    MaxMindDBEnv MM_ISP ISP_DB/isp
    MaxMindDBEnv MM_ORG ISP_DB/organization

    MaxMindDBNetworkEnv ISP_DB ISP_DB_NETWORK
</IfModule>

Blocking by Country

This example shows how to block users based on their country:

MaxMindDBEnable On
MaxMindDBFile COUNTRY_DB /usr/local/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBEnv MM_COUNTRY_CODE COUNTRY_DB/country/iso_code

SetEnvIf MM_COUNTRY_CODE ^(RU|DE|FR) BlockCountry
Deny from env=BlockCountry

Note that at least the "Deny" or "Allow" directive (or "Require" directive in Apache 2.4 and above) must be applied within a <Directory>, <Location> or <Files> container.

Data Output Format

All data is provided as a string bound to the specified Apache environment variable. Floating point numbers are provided to five digits after the decimal place. All integers types except 128-bit integers are provided as decimal. 128-bit integers are returned as hexadecimal. Booleans are returned as "0" for false and "1" for true.

Note that data stored as the "bytes" type in a MaxMind DB database can contain null bytes and may end up truncated when stored in an environment variable. If you really need to access this data, we recommend using one of our programming language APIs instead.

Support

Please report all issues with this code using the GitHub issue tracker.

If you are having an issue with a commercial MaxMind database that is not specific to this module, please see our support page.

Versioning

The MaxMind DB Apache module uses Semantic Versioning.

Copyright and License

This software is Copyright (c) 2013-2021 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.

More Repositories

1

GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
PHP
2,325
star
2

GeoIP2-python

Python code for GeoIP2 webservice client and database reader
Python
1,107
star
3

libmaxminddb

C library for the MaxMind DB file format
C
913
star
4

GeoIP2-java

Java API for GeoIP2 webservice client and database reader
Java
781
star
5

geoipupdate

GeoIP update client code
Go
723
star
6

MaxMind-DB-Reader-php

PHP Reader for the MaxMind DB Database Format
PHP
639
star
7

geoip-api-php

DEPRECATED GeoIP Legacy PHP API
PHP
523
star
8

geoip-api-c

DEPRECATED GeoIP Legacy C API
C
371
star
9

GeoIP2-dotnet

MaxMind GeoIP2 .NET API
C#
348
star
10

web-service-common-php

Shared code for the MaxMind Web Service PHP client APIs
PHP
288
star
11

MaxMind-DB

Spec and test data for the MaxMind DB file format
Go
277
star
12

geoipupdate-legacy

GeoIP update client code
C
258
star
13

geoip-api-python

DEPRECATED GeoIP Legacy Python API
C
232
star
14

GeoIP2-node

Node.js API for GeoIP2 webservice client and database reader
TypeScript
223
star
15

geoip2-csv-converter

GeoIP2 CSV Format Converter
Go
202
star
16

MaxMind-DB-Reader-python

Python MaxMind DB reader extension
Python
178
star
17

geoip-api-java

DEPRECATED GeoIP Legacy Java API
Java
176
star
18

mmdbinspect

look up records for one or more IPs/networks in one or more .mmdb databases
Go
130
star
19

MaxMind-DB-Reader-java

Java reader for the MaxMind DB format
Java
114
star
20

mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Go
114
star
21

MaxMind-DB-Reader-dotnet

.NET Reader for the MaxMind DB Database Format
C#
102
star
22

MaxMind-DB-Writer-perl

Create MaxMind DB database files
Perl
75
star
23

GeoIP2-ruby

Ruby API for GeoIP2 webservice client and database reader
Ruby
59
star
24

geoip-api-mod_geoip2

DEPRECATED GeoIP Legacy module for Apache 2
C
50
star
25

minfraud-api-php

PHP API for minFraud Score, Insights, and Factors
PHP
49
star
26

geoip-api-csharp2

DEPRECATED GeoIP Legacy C# API
C#
47
star
27

MaxMind-DB-Reader-ruby

Ruby reader for the MaxMind DB Database Format
Ruby
46
star
28

getting-started-with-mmdb

A quick guide to writing and reading from your own MMDB databases.
Perl
37
star
29

minfraud-api-python

Python API for minFraud Score, Insights, and Factors
Python
27
star
30

mmdb-from-go-blogpost

Enriching MMDB files with your own data using Go.
Go
23
star
31

ccfd-api-php

Deprecated minFraud Legacy PHP API
PHP
23
star
32

minfraud-api-dotnet

.NET API for MaxMind minFraud Score, Insights, and Factors
C#
20
star
33

minfraud-api-java

Java API for minFraud Score, Insights, and Factors
Java
19
star
34

GeoIP2-perl

Perl API for MaxMind's GeoIP2 web services and databases
Perl
18
star
35

mm-geofeed-verifier

Verify the format of a geofeed file, and make some comparisons to data in an MMDB file.
Go
16
star
36

minfraud-api-ruby

Ruby API for minFraud Score, Insights, and Factors
Ruby
14
star
37

minfraud-api-node

Node.js API for MaxMind minFraud Score, Insights, and Factors
TypeScript
14
star
38

mm-network-analyzer

A program to aid in diagnosing networking issues
Go
13
star
39

dev-hire-homework

A homework exercise for engineering applicants
Perl
13
star
40

MaxMind-DB-Reader-perl

Read MaxMind DB files and look up IP addresses
Perl
12
star
41

mmdbverify

Verifier for the MaxMind DB format
Go
10
star
42

geoip-api-perl

DEPRECATED GeoIP Legacy Perl API
Perl
10
star
43

Stepford

A vaguely Rake/Make/Cake-like thing for Perl - create steps and let a runner run them
Perl
9
star
44

ccfd-api-java

Deprecated minFraud Legacy Java API
Java
7
star
45

Locale-Country-Multilingual

mapping ISO codes to localized country names
Perl
6
star
46

Database-Migrator

Mirror of Database-Migrator on urth.org
Perl
5
star
47

Net-Works

Sane APIs for IP addresses and networks
Perl
5
star
48

MaxMind-DB-Reader-XS

Fast XS implementation of MaxMind DB reader
Perl
5
star
49

dev-site

Static site generator for https://dev.maxmind.com.
HTML
4
star
50

xgb2code

A converter for xgboost model dumps to code.
Go
4
star
51

ccfd-api-asp

minFraud ASP API
ASP
3
star
52

webservice-paypal-paymentsadvanced

A simple wrapper around the PayPal Payments Advanced web service
Perl
3
star
53

gatling-gen

C++
2
star
54

geoip-api-mscom

DEPRECATED GeoIP Legacy MS COM API
C
2
star
55

App-CISetup

Command line tools to generate and update Travis and AppVeyor configs for Perl libraries
Perl
2
star
56

api-specs

TypeScript
2
star
57

MaxMind-DB-Common-perl

Code shared by the MaxMind DB reader and writer modules
Perl
2
star
58

WebService-PivotalTracker

Perl library for the Pivotal Tracker REST API
Perl
2
star
59

minfraud-api-perl

Perl API for minFraud Score, Insights, and Factors
Perl
2
star
60

TAP-Formatter-TeamCity

Emit test results as TeamCity build messages
Perl
2
star
61

geolite2-ws-blogpost

Integrating MaxMind's Free and Paid IP Geolocation Web Services (in PHP)
PHP
2
star
62

fuzzing-workshop

Code for Summit Fuzzing Workshop
Go
1
star
63

Dist-Zilla-PluginBundle-MAXMIND

Perl
1
star
64

blog-site

Static site generator for https://blog.maxmind.com.
SCSS
1
star
65

TeamCity-Message

Generate TeamCity build messages
Perl
1
star
66

xgbshap

Calculates feature contributions for XGBoost models
Go
1
star