Nginx Rewrite Generator
Generate Nginx rewrite rules for URLs, paths, redirects, query strings, variables, $1/$2 captures, Kubernetes ingress paths, and 80 to 443 redirects.
Quick Summary
What is this tool?
An Nginx rewrite rule alters the incoming request URI based on regular expressions (PCRE). It can either rewrite the path internally (so the user's browser URL remains unchanged) or issue an HTTP 301/302 redirect to force the browser to a new URL.
Capturing groups like $1 and $2 are used to extract parts of the old URL (like a blog ID or category) and seamlessly inject them into the new URL.
How to Use This Tool
- Select Rule Type: Choose between an internal rewrite, a permanent redirect (301), or a temporary redirect (302).
- Define the Pattern: Enter the source path regex (e.g.,
^/old-blog/(.*)$). - Define the Target: Enter the new path, using capture variables like
$1(e.g.,/target-path/$1). - Set Flags: Check options like preserving query strings (
$args), or appending flags likelastorbreak. - Export: Copy the generated location block or
rewritedirective into your Nginx configuration.
What This Tool Generates
- Nginx
rewriterule - Nginx
return 301/302redirect rule - Nginx
locationblock - Kubernetes ingress rewrite annotation where supported
- README test notes
Example Output Explanation
A safe example of a 301 permanent redirect preserving the path variable:
location /old/ {
rewrite ^/old/(.*)$ /new/$1 permanent;
}Best Practices
- Nginx rewrite variables: Understand that $1 captures the first set of parentheses in your regex, $2 the second. $request_uri contains the full original path and query string, while $args contains just the query parameters.
- Nginx rewrite without redirect: If you use the 'last' or 'break' flag instead of 'permanent' or 'redirect', Nginx processes the new URI internally. The user's browser will still display the old URL.
- Query string section: By default, Nginx appends the original query string to the rewritten URL. To drop the query string entirely, add a question mark (?) to the end of the replacement string.
Common Mistakes
- Redirect loops: Creating a rewrite rule where the target matches the source regex, causing an infinite 301 loop in the browser.
- Open redirects: Trusting user input or unvalidated headers in a return statement, which can lead to security vulnerabilities.
- Breaking SEO with wrong permanent redirects: Using a 302 (temporary) instead of a 301 (permanent) when moving a page permanently. Search engines will not update their indexes for a 302.
Security Notes
- Always test complex regex locally or in staging first to ensure it doesn't accidentally capture unintended routes.
- Use placeholders for domains and private infrastructure values when asking AI tools or forums for regex help.
- Check canonical URLs after implementing rewrites to ensure search engines aren't confused by duplicate routing.
Testing Instructions
- Always test syntax before reloading: 'nginx -t'
- Reload safely without dropping connections: 'sudo systemctl reload nginx'
Frequently Asked Questions
What is an Nginx Rewrite Generator?
How do I create an Nginx rewrite rule?
What does $1 mean in Nginx rewrite?
How do I rewrite URL without redirect?
How do I preserve query parameters in Nginx rewrite?
How do I redirect HTTP 80 to HTTPS 443 in Nginx?
How We Keep Your Configs Safe & Valid
Built-in Error Checking
Every file is checked against official rules. We catch missing fields and bad syntax. YAML indentation errors are flagged right away. Kubernetes, Terraform, and Docker specs are all covered. API versions and labels are verified too. You get valid output every time you generate.
100% Private & Local
All tools run in your browser only. Your API keys never leave your machine. We do not use any tracking scripts. No data is sent to any server. Passwords and secrets stay on your device. Crypto operations use the Web Crypto API. Your privacy is fully protected at all times.
Secure Settings by Default
Configs use safe defaults out of the box. Containers run as non-root users. Root filesystems are set to read-only. Dangerous Linux capabilities are dropped. Network policies limit pod-to-pod traffic. TLS 1.3 is enabled for web servers. Security headers are added where needed.
Ready for CI/CD & Git
Output files are ready for your Git repo. Use them with ArgoCD, Flux, or GitHub Actions. Files use clear formatting and comments. Code review is easy for your team. Indentation and key order are consistent. Test in staging before going to production. Every file is clean and well-structured.
Infrastructure as Code
Store configs in Git alongside your code. Terraform modules include typed variables. Backend configs support remote state locking. Outputs work across multiple modules. Ansible playbooks use clear task steps. Chef and Puppet configs are also supported. Every file works with version control tools.
Monitoring & Tracing
Set up Prometheus with auto-discovery rules. Create Grafana dashboards with template variables. Add alerting rules with severity labels. Use OpenTelemetry for trace collection. Forward logs to Loki or Elasticsearch. Connect to Jaeger or Tempo for tracing. Monitor metrics, logs, and traces together.
Container & Docker Safety
Dockerfiles use multi-stage builds for small images. Base images are pinned to exact versions. Dev files are excluded from final images. Health checks are added for orchestrator use. Containers switch to non-root users. Docker Compose uses named volumes and networks. Resource limits are set in deploy configs.
Multiple Output Formats
Export as YAML, JSON, HCL, or TOML. Kubernetes uses YAML with proper separators. Terraform uses HCL with correct escaping. JSON output has consistent indentation. Copy to clipboard with one click. Preview output with syntax highlighting. Line numbers help you review quickly.