ConfigGenerator

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.

Output:A ready-to-use configuration file for Nginx Rewrite with best practices applied.

Global Redirections & SEO

Rewrite & Redirect Rules

Single Page App (SPA) Fallback

Quick Summary

Use an Nginx Rewrite Generator when you need to create rewrite or redirect rules for paths, URLs, query strings, capture groups, HTTP to HTTPS redirects, legacy URLs, or Kubernetes Nginx ingress path rewrites.

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

  1. Select Rule Type: Choose between an internal rewrite, a permanent redirect (301), or a temporary redirect (302).
  2. Define the Pattern: Enter the source path regex (e.g., ^/old-blog/(.*)$).
  3. Define the Target: Enter the new path, using capture variables like $1 (e.g., /target-path/$1).
  4. Set Flags: Check options like preserving query strings ($args), or appending flags like last or break.
  5. Export: Copy the generated location block or rewrite directive into your Nginx configuration.

What This Tool Generates

  • Nginx rewrite rule
  • Nginx return 301/302 redirect rule
  • Nginx location block
  • 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?
It's a developer tool that automatically builds the complex PCRE regular expressions and Nginx configuration syntax required to map old URLs to new ones, issue HTTP redirects, or manipulate query strings.
How do I create an Nginx rewrite rule?
You define a location block in your nginx.conf, then use the 'rewrite' directive followed by a regex pattern, a replacement string, and an optional flag like 'permanent' (for 301) or 'last' (for internal routing).
What does $1 mean in Nginx rewrite?
$1 is a capture variable. It contains the exact string of text that matched the first set of parentheses (.*) in your rewrite regex pattern. This allows you to dynamically pass IDs, slugs, or paths from the old URL to the new one.
How do I rewrite URL without redirect?
Use an internal rewrite by applying the 'last' or 'break' flag instead of 'permanent' or 'redirect'. For example: rewrite ^/app/(.*)$ /internal.php?path=$1 last;
How do I preserve query parameters in Nginx rewrite?
Nginx preserves query parameters automatically by default when using the rewrite directive. If you use a 'return 301', you must manually append '$is_args$args' to your target URL to preserve them.
How do I redirect HTTP 80 to HTTPS 443 in Nginx?
Create a server block listening on port 80 with the directive: return 301 https://$host$request_uri; This safely redirects all traffic to HTTPS while preserving the exact path and query string.

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.

Related Tools