Skip to main content

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 map module works for clean conditional logic
  • How to use ngx_stream for 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


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 301 for permanent redirects — it is faster than rewrite
  • Use map instead of nested if blocks
  • Keep rewrite rules in server blocks when possible, not location
  • Use nginx -s reload for graceful reloads — never kill -9 the 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.