user159925
user159925

Reputation: 339

Apply directive in nginx.conf only if module is installed (apache ifmodule alternative)

Is there any alternative to the ifmodule directive in apache?

I'm trying to create a generic nginx.conf file for our company, but some sites have special requirements as having the geoip module. In these sites, I need to declare a directive in nginx.conf, http section, to define the country database.

The problem is that in other servers, that module is not compilled, so I need a caveat to load it only if that module is installed.

Any idea? Thanks in advance!

Upvotes: 4

Views: 2624

Answers (1)

Michael Tabolsky
Michael Tabolsky

Reputation: 3597

Sorry to disappoint, it is not possible, not with today nginx. It was said more than once via different forums and mailing lists that nginx config doesn't support macros or conditional includes. Which is what you are after. The config, except for some minimal things is pretty static in nginx. you can't "program" it as you did on apache. I'll be happy to learn I am wrong, but ATM we all resort to hacks. Depends on your environment but if you control the servers, you can put the geoip into a separate directory and use globbing to include. That is, don't put any file there on the servers without geoip and put there a config file with geoip config if the module is present. If this is not an option you probably have to modify the init script to do the checks and adapt the config.

Example:

assume you have a directory: /etc/nginx/geoip/

on the server with geoip module available there is a file inside the directory:default.http.scope

the content of the file:

geoip_country /usr/share/GeoIP/GeoIP.dat

on the servers without geoip module, the directory is empty.

In the nginx config at the http scope you can safely do:

include /etc/nginx/geoip/*.http.scope

on both servers and only the one with the files that match the pattern will try to configure geoip.

Upvotes: 6

Related Questions