-
-
Notifications
You must be signed in to change notification settings - Fork 0
Module Loading
This guide explains how to properly load the nginx-torblocker module in various Nginx configurations and environments.
The nginx-torblocker module is a dynamic module that must be loaded explicitly using the load_module directive. This guide covers different loading scenarios and best practices.
The load_module directive must be placed at the top level of your nginx.conf file, before any other configuration blocks:
# Must be at the very top of nginx.conf
load_module modules/ngx_http_torblocker_module.so;
# Then your configuration blocks
events {
worker_connections 1024;
}
http {
# Your HTTP configuration
}# Load the module first
load_module modules/ngx_http_torblocker_module.so;
# Optional: Load other modules
load_module modules/ngx_http_geoip_module.so;
load_module modules/ngx_http_image_filter_module.so;
# Main configuration
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Now you can use torblock directives
torblock on;
# Include server configurations
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}Use the relative path which works with standard Nginx installations:
load_module modules/ngx_http_torblocker_module.so;This assumes the module is in Nginx's standard modules directory:
-
Ubuntu/Debian:
/usr/lib/nginx/modules/ -
CentOS/RHEL:
/usr/lib64/nginx/modules/ - Custom builds: Relative to Nginx prefix
Use absolute path if you have a custom installation:
# Ubuntu/Debian
load_module /usr/lib/nginx/modules/ngx_http_torblocker_module.so;
# CentOS/RHEL
load_module /usr/lib64/nginx/modules/ngx_http_torblocker_module.so;
# Custom installation
load_module /opt/nginx/modules/ngx_http_torblocker_module.so;If you store modules in a custom location:
load_module /etc/nginx/modules/ngx_http_torblocker_module.so;For simple setups, load the module in your main nginx.conf:
# /etc/nginx/nginx.conf
load_module modules/ngx_http_torblocker_module.so;
events {
worker_connections 1024;
}
http {
torblock on;
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}For complex setups with multiple configuration files:
Main configuration (/etc/nginx/nginx.conf):
# Load all modules at the top
load_module modules/ngx_http_torblocker_module.so;
load_module modules/ngx_http_ssl_module.so;
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Global torblock settings
include /etc/nginx/conf.d/torblock.conf;
# Include site configurations
include /etc/nginx/sites-enabled/*.conf;
}Torblock configuration (/etc/nginx/conf.d/torblock.conf):
# Global Tor blocking configuration
torblock on;
torblock_update_interval 3600000;
# Logging configuration
log_format torblock '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'torblock="$torblock_status"';Site configuration (/etc/nginx/sites-enabled/example.conf):
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.log torblock;
location / {
root /var/www/example;
index index.html;
}
location /admin {
torblock on; # Override global if needed
root /var/www/admin;
}
}If Nginx was installed via apt:
# Standard path for apt-installed Nginx
load_module modules/ngx_http_torblocker_module.so;Module should be copied to: /usr/lib/nginx/modules/
If Nginx was installed via yum/dnf:
# Standard path for yum-installed Nginx
load_module modules/ngx_http_torblocker_module.so;Module should be copied to: /usr/lib64/nginx/modules/
For Docker containers:
Dockerfile:
FROM nginx:alpine
# Copy the module
COPY ngx_http_torblocker_module.so /usr/lib/nginx/modules/
# Copy configuration
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]nginx.conf:
load_module modules/ngx_http_torblocker_module.so;
events {
worker_connections 1024;
}
http {
torblock on;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}If you compiled Nginx from source:
# Check your Nginx prefix
nginx -V 2>&1 | grep -o '\--prefix=[^ ]*'
# Example output: --prefix=/usr/local/nginxThen use the appropriate path:
# For prefix=/usr/local/nginx
load_module modules/ngx_http_torblocker_module.so;
# Module should be in: /usr/local/nginx/modules/Find where Nginx expects modules:
# Check Nginx configuration
nginx -V 2>&1 | grep -o '\--modules-path=[^ ]*'
# List current modules directory
ls -la $(nginx -V 2>&1 | grep -o '\--modules-path=[^ ]*' | cut -d= -f2)
# Alternative: Find the directory
find /usr -name "modules" -type d 2>/dev/null | grep nginxTest if the module loads correctly:
# Test configuration syntax
sudo nginx -t
# Expected success output:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successfulMonitor error logs during startup:
# Watch error log during reload
sudo tail -f /var/log/nginx/error.log &
sudo nginx -s reload
# Look for module-related messages
sudo grep -i "torblock\|module" /var/log/nginx/error.logTest that directives are recognized:
# Create test configuration
echo "torblock on;" | sudo nginx -t -c /dev/stdin
# Should not show "unknown directive" errornginx: [emerg] dlopen() "/usr/lib/nginx/modules/ngx_http_torblocker_module.so" failed (2: No such file or directory)
Solutions:
-
Check if file exists:
ls -la /usr/lib/nginx/modules/ngx_http_torblocker_module.so
-
Find correct modules directory:
find /usr -name "*.so" -path "*/nginx/modules/*" 2>/dev/null
-
Copy module to correct location:
sudo cp ngx_http_torblocker_module.so /usr/lib/nginx/modules/
nginx: [emerg] module "/usr/lib/nginx/modules/ngx_http_torblocker_module.so" version 1024000 instead of 1026000
Solutions:
-
Check Nginx version:
nginx -v
-
Rebuild module against correct version:
# Download matching Nginx source and rebuild wget https://nginx.org/download/nginx-1.26.0.tar.gz # Follow build instructions
nginx: [emerg] unknown directive "torblock" in /etc/nginx/nginx.conf:15
Solutions:
-
Ensure load_module is first:
# Must be at very top load_module modules/ngx_http_torblocker_module.so; events { # ... }
-
Check module path:
sudo nginx -T | grep load_module
nginx: [emerg] dlopen() failed (13: Permission denied)
Solutions:
-
Fix file permissions:
sudo chmod 644 /usr/lib/nginx/modules/ngx_http_torblocker_module.so
-
Check SELinux (CentOS/RHEL):
sudo setsebool -P httpd_execmem 1
-
Check directory permissions:
sudo chmod 755 /usr/lib/nginx/modules/
# Good: Works across different installations
load_module modules/ngx_http_torblocker_module.so;
# Avoid: Hard-coded absolute paths
load_module /usr/lib/nginx/modules/ngx_http_torblocker_module.so;# Load all modules at the top
load_module modules/ngx_http_torblocker_module.so;
load_module modules/ngx_http_geoip_module.so;
load_module modules/ngx_http_realip_module.so;
# Then configuration
events {
# ...
}# Security modules
load_module modules/ngx_http_torblocker_module.so; # Tor blocking
load_module modules/ngx_http_limit_req_module.so; # Rate limiting
# Performance modules
load_module modules/ngx_http_gzip_module.so; # CompressionUse environment variables for different deployments:
# Development
load_module modules/ngx_http_torblocker_module.so;
# Production might load additional security modules
load_module modules/ngx_http_torblocker_module.so;
load_module modules/ngx_http_modsecurity_module.so;- name: Copy Tor blocker module
copy:
src: ngx_http_torblocker_module.so
dest: /usr/lib/nginx/modules/
mode: '0644'
owner: root
group: root
- name: Configure Nginx with Tor blocker
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: reload nginxversion: '3.8'
services:
nginx:
image: nginx:alpine
volumes:
- ./ngx_http_torblocker_module.so:/usr/lib/nginx/modules/ngx_http_torblocker_module.so:ro
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "80:80"After successfully loading the module:
- Basic Setup: Continue with Basic Configuration
- Advanced Usage: Explore Advanced Configuration
- Troubleshooting: Review Troubleshooting Guide if needed
- Development: See Development Setup for building custom versions