Advanced Features
This module covers the expert-level capabilities that make Nginx a complete platform — beyond basic web serving.
What You Will Learn
- How to write rewrite rules and redirects
- How the
mapmodule works for clean conditional logic - How to use
ngx_streamfor TCP/UDP proxying (non-HTTP traffic) - Full CLI management — config testing, reloads, signals
- Production bash scripts for automation
- opencode AI workflows for Nginx config generation and debugging
Topics in This Module
- Rewrite Rules and Redirects —
return,rewrite, regex redirects, 301 vs 302 - Map Module —
mapfor conditional variables, cleaner thanifblocks - Stream Module (TCP Proxying) — Proxying non-HTTP protocols (MySQL, Redis, custom TCP)
- CLI Management — All Nginx CLI commands, signals, config testing
- Script Automation — Bash scripts for health checks, site provisioning, backup, reload
- opencode AI Workflows — AI-assisted config generation, log analysis, debugging
Why Avoid if in Nginx
Nginx's if directive inside location blocks has subtle and dangerous behavior. The Nginx docs themselves call it "evil". Use map for most conditional logic instead:
# BAD — if inside location
location / {
if ($request_method = POST) {
return 405;
}
}
# BETTER — map at http level
map $request_method $is_post {
POST 1;
default 0;
}
Best Practices
- Use
return 301for permanent redirects — it is faster thanrewrite - Use
mapinstead of nestedifblocks - Keep rewrite rules in
serverblocks when possible, notlocation - Use
nginx -s reloadfor graceful reloads — neverkill -9the master - Store automation scripts in
/etc/nginx/scripts/or/usr/local/bin/
Success Checkpoint
By the end of this module you should be able to write clean rewrite rules, use map for conditional config, proxy TCP traffic with the stream module, and have a working set of automation scripts.