URL Rewriting in Nginx
From the Nginx cheat sheet · Location Blocks & Rewrites · verified Jul 2026
URL Rewriting
Rewriting and redirecting URLs
nginx
# Simple redirect
location /old {
return 301 /new;
}
# Rewrite rule
location /api {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://backend;
}
# Conditional rewrite
if ($request_uri ~* "/old/(.*)") {
return 301 /new/$1;
}🔄 rewrite changes URI internally
↪️ return sends redirect to client
🏁 break stops processing, last continues
🎯 Use try_files instead of if when possible