36

Introduction

From NGINX version 1.9.11 and upwarts, a new feature is introduced: dynamic modules.

With dynamic modules, you can optionally load separate shared object files at runtime as modules – both third-party modules and some native NGINX modules. (source)

My setup and the problem

I have NGINX installed from the mainline (currently 1.9.14) so it is capable to use dynamic modules. It has also the module I want dynamicly enabled:

nginx -V
nginx version: nginx/1.9.14
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1) 
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules ... --with-http_geoip_module=dynamic ...

Note the --with-http_geoip_module=dynamic which loads the module I need (dynamically). Unfortunately, the documentation is lacking (some details) and I am unable to set this up.
I have an existing NGINX installation (not from source). But so far as I can understand I just need to build the module, place the generated module file in the right NGINX folder and enable it in the config file.

What I tried so far

I tested this on a different machine (with the same configuration, but not a production machine), but I don't see the ngx_http_geoip_module.so file. The commands I used:

wget http://nginx.org/download/nginx-1.9.14.tar.gz
tar -xzf nginx-1.9.14.tar.gz
cd nginx-1.9.14/
./configure --with-http_geoip_module=dynamic

The questions

  • Is it a problem that I try to build the module on a system that has NGINX installed not from source?
  • Why is there no .so file generated by my commands?
yoano
  • 1,466
  • 2
  • 16
  • 20

4 Answers4

38

I had the same question, and @vladiastudillo answer was the missing piece I needed.

First add the nginx stable repo:

sudo add-apt-repository ppa:nginx/stable

Then run apt update:

sudo apt-get update

And get the nginx geoip module:

sudo apt-get install nginx-module-geoip

This will download and load the module to /usr/lib/nginx/modules


To load the nginx module,

open nginx.conf:

sudo nano /etc/nginx/nginx.conf

add add below in the main context:

load_module "modules/ngx_http_geoip_module.so";

The module will be loaded, when you reload the configuration or restart nginx.

To dynamically “unload” a module, comment out or remove its load_module directive and reload the nginx configuration.

miyuru
  • 1,141
  • 11
  • 19
  • 2
    Do I need to reinstall nginx from the ppa? Or is the standard NGINX from Ubuntu sufficient? – yoano Aug 01 '16 at 08:49
  • 1
    if the NGINX is built with dynamic modules you don't need to reinstall. To check available modules type `nginx -V` and check for the dynamic string. – miyuru Aug 12 '16 at 06:46
  • Where is the main context? – W.M. Feb 13 '17 at 16:30
  • 1
    @W.M. main context is in the nginx.conf file, without any brackets. add it as the first line. – miyuru Jun 18 '18 at 05:31
  • 1
    did any one get a problem due to version differences i.e., you are running version say 1.13 and the module is compiled against version say 1.17? – OAH Aug 10 '20 at 04:05
8

Found this to be slightly different on Amazon Linux 2016.09, Amazon Linux 2016.03 after performing yum update.

You can confirm this ahead of time by using this command on your ec2 instance sudo yum search nginx-mod-http-geoip and you will see an N/S matched: nginx-mod-http-geoip entry in the response with specifics of nginx-mod-http-geoip.x86_64 : Nginx HTTP geoip module

In these cases, the installed nginx version will be 1.10.1. When this is true, you can simple install the nginx geoip module from Amazon's existing yum repo via:

sudo yum install nginx-mod-http-geoip

Then associate the module with your nginx.conf and placing this line in the main context

include /usr/share/nginx/modules/mod-http-geoip.conf;

(note this is subtly different from the main answer - in aws you have an entry in nginx.conf pointing to another *.conf file which then points to the *.so file)

Peter Jones
  • 81
  • 1
  • 2
  • I think I have a similar issue to yours, but I am hosting with Vultr, and I when I run `nginx -t` I get: `nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014000 in /usr/share/nginx/modules/mod-http-geoip.conf:1` When I tried your solution, it just said that it was already installed! Any ideas? – J86 Apr 25 '18 at 20:30
  • 2018/07/09 09:37:14 [emerg] 9552#0: "load_module" directive is not allowed here in /usr/share/nginx/modules/mod-http-geoip.conf:1 Any ideas why i get this? I followed your steps exactly.. – user1955934 Jul 09 '18 at 10:46
  • i need help: https://stackoverflow.com/questions/51243269/nginx-geoip-module-config-issue – user1955934 Jul 09 '18 at 16:58
  • 1
    `No package nginx-mod-http-geoip available.` . ? anyone has this issue ? – Attila Naghi Feb 04 '20 at 08:59
4

If you are using docker nginx:latest this module is already included in the image as such you only need specify load_module as such:

"/usr/lib/nginx/modules/ngx_http_geoip_module.so";

You also need to create a geoip folder in your nginx mapped volume. Though the databases seem to no longer been updated or available and geoip2 modules are not included. So you many need to google...

2

I had the same issue, you have to install the http_geoip_module lib for your ubuntu version with:

$ sudo apt-get install nginx-module-geoip

vladiastudillo
  • 407
  • 1
  • 10
  • 23
  • 3
    I tried your command, but I got "Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package nginx-module-geoip" – yoano May 03 '16 at 09:44