Welcome to Part 20 of our comprehensive NGINX on Ubuntu series! We’ll explore edge computing and IoT applications, deploying NGINX at the network edge for low-latency, distributed computing scenarios.
Edge Computing Fundamentals
Edge computing brings computation and data storage closer to IoT devices and end-users, reducing latency, bandwidth usage, and improving real-time processing capabilities.
graph TD A[Edge Computing Architecture] --> B[Edge Nodes] A --> C[IoT Gateway] A --> D[Data Processing] A --> E[Cloud Integration] B --> F[NGINX EdgeLocal CachingLoad Balancing] C --> G[Device ManagementProtocol TranslationSecurity Gateway] D --> H[Real-time AnalyticsML InferenceData Filtering] E --> I[Cloud SyncBackup StorageGlobal Coordination] J[Edge Benefits] --> K[Low Latency] J --> L[Bandwidth Savings] J --> M[Real-time Processing] J --> N[Offline Capability] style A fill:#e1f5fe style J fill:#e8f5e8 style F fill:#fff3e0 style G fill:#e3f2fd style H fill:#e8f5e8 style I fill:#fff3e0
NGINX Edge Gateway Configuration
# Create edge computing project structure
sudo mkdir -p /opt/nginx-edge/{configs,scripts,data,logs}
cd /opt/nginx-edge
# Create edge-optimized NGINX configuration
cat > configs/nginx-edge.conf << 'EOF'
# NGINX Edge Computing Configuration
user www-data;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 32768;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 2048;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Optimized for edge computing
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 100;
# Reduced buffer sizes for edge devices
client_body_buffer_size 32k;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 1m;
# Edge caching zones
proxy_cache_path /var/cache/nginx/edge-hot
levels=1:1
keys_zone=edge_hot:50m
max_size=500m
inactive=5m
use_temp_path=off;
proxy_cache_path /var/cache/nginx/edge-warm
levels=1:2
keys_zone=edge_warm:100m
max_size=2g
inactive=1h
use_temp_path=off;
# Logging
log_format edge '$remote_addr [$time_local] "$request" $status '
'$body_bytes_sent rt=$request_time '
'cache=$upstream_cache_status edge_id=$hostname';
access_log /opt/nginx-edge/logs/access.log edge buffer=16k flush=5s;
error_log /opt/nginx-edge/logs/error.log warn;
# Compression optimized for IoT
gzip on;
gzip_vary on;
gzip_min_length 256;
gzip_comp_level 4;
gzip_types application/json text/plain application/javascript;
include /opt/nginx-edge/configs/sites/*.conf;
}
EOF
IoT Gateway Configuration
# Create IoT gateway configuration
mkdir -p configs/sites
cat > configs/sites/iot-gateway.conf << 'EOF'
# IoT Gateway Configuration
upstream iot_processors {
server 127.0.0.1:8001 weight=3;
server 127.0.0.1:8002 weight=2;
keepalive 16;
}
upstream cloud_backend {
server cloud.iot-platform.com:443;
keepalive 8;
}
# Main IoT Gateway
server {
listen 80;
listen 443 ssl http2;
server_name iot-gateway.local;
# SSL for secure IoT communication
ssl_certificate /opt/nginx-edge/ssl/gateway.crt;
ssl_certificate_key /opt/nginx-edge/ssl/gateway.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20:!aNULL:!SHA1:!WEAK;
# Device data ingestion
location /api/devices/data {
limit_req zone=device_data burst=100 nodelay;
limit_conn device_conn 50;
proxy_pass http://iot_processors;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Edge-Gateway $hostname;
proxy_set_header X-Device-Type $http_x_device_type;
# Fast timeouts for IoT
proxy_connect_timeout 2s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
# No caching for real-time data
proxy_no_cache 1;
proxy_cache_bypass 1;
}
# Device configuration and commands
location /api/devices/config {
proxy_cache edge_hot;
proxy_cache_valid 200 5m;
proxy_cache_key "$request_uri$http_x_device_id";
proxy_pass http://iot_processors;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Edge-Gateway $hostname;
add_header X-Cache-Status $upstream_cache_status always;
}
# Cloud synchronization
location /api/cloud/sync {
proxy_pass https://cloud_backend/edge-sync;
proxy_ssl_server_name on;
proxy_ssl_verify off;
proxy_set_header Host cloud.iot-platform.com;
proxy_set_header X-Edge-ID $hostname;
proxy_set_header Authorization "Bearer $http_authorization";
proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# WebSocket support for real-time device communication
location /ws/devices {
proxy_pass http://iot_processors;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
# Edge analytics dashboard
location /dashboard {
root /opt/nginx-edge/public;
try_files $uri $uri/ /dashboard/index.html;
# Cache static assets
location ~* \.(js|css|png|jpg|svg)$ {
expires 1h;
add_header Cache-Control "public";
}
}
# Health check for edge node
location /health {
access_log off;
return 200 '{"status":"healthy","edge_id":"$hostname","timestamp":"$time_iso8601"}\n';
add_header Content-Type application/json;
}
}
# Rate limiting zones
limit_req_zone $binary_remote_addr zone=device_data:10m rate=100r/s;
limit_conn_zone $binary_remote_addr zone=device_conn:10m;
EOF
Edge Data Processing
graph TD A[IoT Device Data] --> B[Edge Gateway] B --> C[Data Validation] C --> D[Local Processing] D --> E[Caching Layer] E --> F[Analytics Engine] F --> G[Decision Logic] G --> H[Device Commands] G --> I[Cloud Sync] J[Edge Processing] --> K[Real-time Analytics] J --> L[ML Inference] J --> M[Data Aggregation] J --> N[Alerting] style A fill:#e1f5fe style J fill:#e8f5e8 style D fill:#fff3e0 style F fill:#e3f2fd style G fill:#e8f5e8
# Create edge data processing configuration
cat > configs/sites/edge-processor.conf << 'EOF'
# Edge Data Processing Service
upstream ml_inference {
server 127.0.0.1:8080; # TensorFlow Serving
server 127.0.0.1:8081; # Custom ML API
}
upstream time_series_db {
server 127.0.0.1:8086; # InfluxDB
}
server {
listen 8001;
server_name edge-processor;
# Sensor data processing
location /process/sensors {
proxy_pass http://ml_inference/predict;
proxy_method POST;
proxy_set_header Content-Type application/json;
proxy_set_header X-Edge-Node $hostname;
proxy_set_header X-Timestamp $time_iso8601;
# Fast processing for real-time requirements
proxy_connect_timeout 1s;
proxy_send_timeout 3s;
proxy_read_timeout 3s;
# Log processing results
access_log /opt/nginx-edge/logs/processing.log edge;
}
# Time series data storage
location /store/metrics {
proxy_pass http://time_series_db/write;
proxy_method POST;
proxy_set_header Authorization "Token $arg_token";
proxy_set_header Content-Type application/x-protobuf;
# Buffer for batch writes
proxy_buffering on;
proxy_buffer_size 64k;
proxy_buffers 8 64k;
}
# Data aggregation endpoint
location /aggregate/data {
proxy_cache edge_warm;
proxy_cache_valid 200 1m;
proxy_cache_use_stale error timeout;
proxy_pass http://time_series_db/query;
# Cache aggregated results briefly
add_header X-Cache-Status $upstream_cache_status;
expires 30s;
}
}
EOF
Edge Management Scripts
# Create edge management script
cat > scripts/edge-manager.sh << 'EOF'
#!/bin/bash
# NGINX Edge Computing Manager
EDGE_DIR="/opt/nginx-edge"
CONFIG_FILE="$EDGE_DIR/configs/nginx-edge.conf"
start_edge_services() {
echo "Starting NGINX Edge services..."
# Create cache directories
sudo mkdir -p /var/cache/nginx/{edge-hot,edge-warm}
sudo chown -R www-data:www-data /var/cache/nginx
# Create log directories
mkdir -p $EDGE_DIR/logs
# Start NGINX with edge configuration
sudo nginx -c $CONFIG_FILE -t && sudo nginx -c $CONFIG_FILE
# Start local processing services
start_processing_services
echo "Edge services started"
}
start_processing_services() {
echo "Starting edge processing services..."
# Start ML inference service (example with Python)
nohup python3 $EDGE_DIR/services/ml_inference.py > $EDGE_DIR/logs/ml.log 2>&1 &
echo $! > $EDGE_DIR/ml_inference.pid
# Start data aggregation service
nohup python3 $EDGE_DIR/services/data_aggregator.py > $EDGE_DIR/logs/aggregator.log 2>&1 &
echo $! > $EDGE_DIR/aggregator.pid
echo "Processing services started"
}
monitor_edge_health() {
echo "=== Edge Node Health Status ==="
# Check NGINX status
if pgrep nginx > /dev/null; then
echo "✅ NGINX: Running"
curl -s http://localhost/health | jq . 2>/dev/null || echo "Health check failed"
else
echo "❌ NGINX: Not running"
fi
# Check processing services
if [ -f $EDGE_DIR/ml_inference.pid ] && kill -0 $(cat $EDGE_DIR/ml_inference.pid) 2>/dev/null; then
echo "✅ ML Inference: Running"
else
echo "❌ ML Inference: Not running"
fi
# Check system resources
echo "--- System Resources ---"
echo "CPU: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')"
echo "Memory: $(free -h | grep Mem | awk '{printf "%s/%s", $3, $2}')"
echo "Disk: $(df -h $EDGE_DIR | tail -1 | awk '{print $5}')"
# Check cache usage
if [ -d /var/cache/nginx ]; then
echo "Cache size: $(du -sh /var/cache/nginx | cut -f1)"
fi
}
sync_with_cloud() {
echo "Syncing edge data with cloud..."
# Sync configuration
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $CLOUD_TOKEN" \
-d '{"edge_id":"'$(hostname)'","status":"online","timestamp":"'$(date -Iseconds)'"}' \
https://cloud.iot-platform.com/edge-sync/status
# Upload aggregated data (example)
if [ -f $EDGE_DIR/data/aggregated_data.json ]; then
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $CLOUD_TOKEN" \
--data @$EDGE_DIR/data/aggregated_data.json \
https://cloud.iot-platform.com/edge-sync/data
fi
echo "Cloud sync completed"
}
edge_cleanup() {
echo "Cleaning up edge resources..."
# Clean old cache files
find /var/cache/nginx -type f -mtime +1 -delete 2>/dev/null
# Rotate logs
if [ -f $EDGE_DIR/logs/access.log ]; then
gzip $EDGE_DIR/logs/access.log
mv $EDGE_DIR/logs/access.log.gz $EDGE_DIR/logs/access-$(date +%Y%m%d).log.gz
touch $EDGE_DIR/logs/access.log
fi
# Clean processed data older than 7 days
find $EDGE_DIR/data -name "*.processed" -mtime +7 -delete 2>/dev/null
echo "Cleanup completed"
}
case "${1:-status}" in
start)
start_edge_services
;;
status)
monitor_edge_health
;;
sync)
sync_with_cloud
;;
cleanup)
edge_cleanup
;;
restart)
sudo nginx -s reload
;;
*)
echo "Usage: $0 {start|status|sync|cleanup|restart}"
;;
esac
# Make executable: chmod +x scripts/edge-manager.sh
EOF
IoT Device Simulator
# Create IoT device simulator
cat > scripts/iot-simulator.py << 'EOF'
#!/usr/bin/env python3
import json
import time
import random
import requests
import threading
from datetime import datetime
class IoTDeviceSimulator:
def __init__(self, device_id, gateway_url="http://localhost"):
self.device_id = device_id
self.gateway_url = gateway_url
self.running = False
def generate_sensor_data(self):
return {
"device_id": self.device_id,
"timestamp": datetime.now().isoformat(),
"temperature": round(random.uniform(18.0, 35.0), 2),
"humidity": round(random.uniform(30.0, 80.0), 2),
"pressure": round(random.uniform(980.0, 1020.0), 2),
"battery": round(random.uniform(0.0, 100.0), 1)
}
def send_data(self):
while self.running:
try:
data = self.generate_sensor_data()
response = requests.post(
f"{self.gateway_url}/api/devices/data",
json=data,
headers={
"X-Device-Type": "sensor",
"X-Device-ID": self.device_id
},
timeout=5
)
if response.status_code == 200:
print(f"Device {self.device_id}: Data sent successfully")
else:
print(f"Device {self.device_id}: Failed to send data - {response.status_code}")
except Exception as e:
print(f"Device {self.device_id}: Error - {e}")
time.sleep(random.uniform(5, 15)) # Send every 5-15 seconds
def start(self):
self.running = True
self.thread = threading.Thread(target=self.send_data)
self.thread.start()
print(f"Device {self.device_id} started")
def stop(self):
self.running = False
if hasattr(self, 'thread'):
self.thread.join()
print(f"Device {self.device_id} stopped")
def main():
devices = []
# Create multiple simulated devices
for i in range(1, 6): # 5 devices
device = IoTDeviceSimulator(f"sensor-{i:03d}")
devices.append(device)
device.start()
try:
print("IoT devices running. Press Ctrl+C to stop...")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping devices...")
for device in devices:
device.stop()
if __name__ == "__main__":
main()
# Make executable: chmod +x scripts/iot-simulator.py
EOF
Testing Edge Deployment
# Deploy and test edge computing setup
# 1. Setup edge environment
sudo mkdir -p /var/cache/nginx/{edge-hot,edge-warm}
sudo chown -R www-data:www-data /var/cache/nginx
# 2. Deploy edge configuration
sudo cp /opt/nginx-edge/configs/nginx-edge.conf /etc/nginx/nginx.conf
sudo cp /opt/nginx-edge/configs/sites/* /etc/nginx/sites-available/
# 3. Enable edge sites
sudo ln -sf /etc/nginx/sites-available/iot-gateway.conf /etc/nginx/sites-enabled/
sudo ln -sf /etc/nginx/sites-available/edge-processor.conf /etc/nginx/sites-enabled/
# 4. Test configuration
sudo nginx -t
# 5. Start edge services
/opt/nginx-edge/scripts/edge-manager.sh start
# 6. Check edge status
/opt/nginx-edge/scripts/edge-manager.sh status
# 7. Simulate IoT devices
python3 /opt/nginx-edge/scripts/iot-simulator.py &
# 8. Test endpoints
curl http://localhost/health
curl -X POST http://localhost/api/devices/data \
-H "Content-Type: application/json" \
-H "X-Device-Type: sensor" \
-d '{"temperature":25.5,"humidity":60.0}'
# 9. Monitor edge performance
watch -n 5 '/opt/nginx-edge/scripts/edge-manager.sh status'
# 10. Test cache performance
curl http://localhost/api/devices/config
curl http://localhost/api/devices/config # Should be cached
What’s Next?
Excellent! You’ve deployed NGINX as an edge computing gateway with IoT device management, real-time data processing, and cloud synchronization capabilities. Your edge infrastructure now supports distributed computing at the network edge.
Coming up in Part 21: NGINX Future Technologies and Emerging Trends
References
This is Part 20 of our 22-part NGINX series. Your NGINX now powers edge computing! Next, we’ll explore future technologies and trends. Questions? Share them in the comments!