Advanced OpenResty Reverse Proxy Configuration with GeoIP, ModSecurity, and Lua Load Balancing

Copy Code Example OpenResty Reverse Proxy Features

This is an OpenResty (Nginx with Lua scripting capabilities) configuration for a reverse proxy that performs several advanced functions, including:

  1. HTTP to HTTPS redirection.
  2. SSL/TLS termination.
  3. Web Application Firewall (WAF) using ModSecurity.
  4. GeoIP-based routing: Directing users to different backend server pools based on their country.
  5. Custom Lua-based load balancing:
    • Health checks for backend servers.
    • Least connections algorithm (weighted) to select a backend.
  6. Custom logging including GeoIP data and selected backend.

Let's break down each file and its components:

nginx.conf (Main OpenResty Configuration)

This file sets up the global Nginx/OpenResty environment.


worker_processes 1;
    

worker_processes 1;: Configures Nginx to use a single worker process. For production, this is usually set to auto or the number of CPU cores.


load_module /usr/local/openresty/nginx/modules/ngx_http_modsecurity_module.so;
load_module /usr/local/openresty/nginx/modules/ngx_http_geoip2_module.so;
    
OpenResty Config Code Blocks
  • Dynamically loads the ModSecurity module (for WAF) and the GeoIP2 module (for IP-based geolocation).

events {
    worker_connections 1024;
}
    
  • worker_connections 1024;: Each worker process can handle up to 1024 simultaneous connections.
http {
    lua_shared_dict backend_stats 10m;
}

Example 2


class WSLService(win32serviceutil.ServiceFramework):
    _svc_name_ = "WSLService"
    _svc_display_name_ = "WSL Background Service"
    

Example 3


# New code block content goes here
    

Comments

Popular posts from this blog

Install and Enable Certbot letsencrypt SSL/TLS Certificate in NGINX Ubuntu 22.04 LTS without using snapd