Recommended Free Tools
Use return for a known redirect or fixed status, rewrite when a regular expression must transform a URI, and try_files when the decision depends on whether a file or directory exists. The crucial flag choice is last versus break: last starts a new location search, while break keeps processing in the current location. The examples below show how those directives are evaluated, how to avoid rewrite loops, and how to translate common Apache patterns into an NGINX configuration.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
What NGINX URL rewriting does
The ngx_http_rewrite_module changes request URIs with PCRE regular expressions, returns redirects and other status responses, and conditionally selects configuration. Rewrite directives can run in a server context and then in the selected location. If a rewrite changes the URI and triggers another location lookup, NGINX repeats that processing. The documented safety limit is 10 rewrite or internal-redirect cycles for one request.
As an Amazon Associate I earn from qualifying purchases.
A rewrite can be internal or external. An internal rewrite changes the URI NGINX uses to choose a handler; the browser keeps the original address. An external redirect sends a response such as 301 or 302, and the browser requests the new address. That distinction affects bookmarks, caches, crawlers and whether a second HTTP request is made.
NGINX executes multiple rewrite directives in the order written. Put narrow, high-confidence matches before broad ones, and decide deliberately whether a changed URI should be looked up against the location tree again.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Choose the right directive
| Need | Best starting point | What the client sees | New location search? | Main caution |
|---|---|---|---|---|
| One known destination, canonical host, HTTP-to-HTTPS policy, blocked path or fixed response | return |
Redirect or status response | Not applicable | Choose the status and canonical scheme deliberately. |
| Transform a URI using captures and a regular expression | rewrite |
Internal URI change unless the replacement or flag creates a redirect | last starts one; break does not |
Ordered rules can overlap or loop. |
| Serve an existing file or directory, otherwise hand off to an application | try_files |
Usually internal routing | Depends on the final fallback | Use it for existence checks instead of a long regex chain. |
The NGINX Community Blog describes return as the simpler general-purpose directive and recommends it instead of rewrite when possible. The practical rule is to begin with the least complex directive that expresses the policy.
Use return for fixed redirects and responses
Syntax and behavior
return takes a status code and, depending on the form, a URL or response text. It is ideal when no pattern matching is required. Because it ends request processing immediately, it is easier to reason about than a regular-expression rule.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →A 301 communicates a permanent redirect; a 302 communicates a temporary one. A permanent redirect can be cached by clients and intermediaries and is normally the signal used when a URL has a lasting canonical replacement. Use a temporary response while a move is being tested or is expected to change back. There is no universal latency or search-ranking percentage that proves one directive is faster than another; the important differences are their documented semantics and the extra request caused by an external redirect.
Canonical host with a dedicated server block
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
This separate-server pattern is the form shown in NGINX conversion guidance for moving a noncanonical host to a canonical one. Replace the host and scheme with the values your deployment actually uses. If TLS is terminated by a load balancer or another proxy, establish which forwarded scheme is authoritative before constructing an HTTPS redirect; otherwise a backend can redirect an already-secure request repeatedly.
Other fixed responses
server {
listen 80;
server_name example.org;
location = /private-preview {
return 403;
}
location = /old-health-endpoint {
return 410;
}
}
Exact locations make a fixed response readable. A return can also include response text where the selected status permits it. Keep unconditional host policies at server level so they do not compete with application locations.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Use rewrite for regular-expression URI transformations
Directive syntax
rewrite regex replacement [flag];
The regular expression is matched against the request URI. Captures can be inserted into the replacement as $1, $2 and so on. If the replacement begins with http://, https:// or $scheme, NGINX returns a redirect rather than merely changing the internal URI.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →The redirect flag produces a temporary 302 response. The permanent flag produces a 301. Without either redirect flag, the rewrite is internal; its final handling depends on whether you choose last, break or leave the default behavior in the applicable context.
Ordered media-path transformation
location /download/ {
rewrite ^(/download/.*)/media/(.*)..*$ $1/mp3/$2.mp3 last;
rewrite ^(/download/.*)/audio/(.*)..*$ $1/mp3/$2.ra last;
}
These rules illustrate two important properties: NGINX evaluates them top to bottom, and the first matching rewrite with last ends that directive set and starts a new location search. The captures preserve the directory and filename portions while changing the media path and extension. If both expressions could match the same URI, put the more specific rule first.
When a regex redirect is appropriate
A regex redirect is useful for a family of old URLs that share a stable pattern. For a single known URL, a literal return is clearer. For a rule whose real question is “does this file exist?”, regex is the wrong abstraction; use try_files instead.
last versus break
| Flag | Effect after the replacement | Use it when | Typical risk |
|---|---|---|---|
last |
Stops the current rewrite set and performs a new location search for the changed URI. | The new URI should be handled by another location or by a different location-specific configuration. | The new search can match the original rule again and create an internal redirection cycle. |
break |
Stops the current rewrite set without starting a new location search. | The URI should remain in the current location and continue to its existing handler. | The current location may not contain the handler you expected for the rewritten URI. |
Suppose a location rewrites /download/file/media/song.wav to /download/file/mp3/song.mp3. Use last if the destination must be evaluated against the location tree again. Use break if the current location already knows how to serve or proxy the destination and another lookup would be unnecessary. The correct choice is architectural, not a performance trick.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteRank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Whenever a last rule can produce a URI that still matches its own location and expression, test the cycle explicitly. NGINX stops after 10 rewrite or internal-redirect cycles, and the error log can report an “internal redirection cycle” or “rewrite or internal redirection cycle” symptom. A cycle limit is a failure guard, not a successful routing strategy.
Use try_files for files, directories and front controllers
Basic fallback pattern
location / {
try_files $uri $uri/ @app;
}
location @app {
proxy_pass http://backend;
}
try_files checks candidates in order. In this example, NGINX first tests the request path as a file, then as a directory, and finally transfers control to the named application location. Adapt the final handler to the application protocol you use, such as proxying to an upstream or passing to a FastCGI service.
This shape is usually clearer than encoding every file and directory case as a regular expression. It also makes the fallback policy visible: existing static content wins, and only a miss reaches the application.
Maintenance and index candidates
location / {
try_files /maintenance.html $uri $uri/index.html $uri.html @app;
}
location @app {
proxy_pass http://backend;
}
The conversion guide’s representative sequence includes a maintenance file, $uri, $uri/index.html, $uri.html and an application fallback. Adjust the order to your site’s policy. Putting the maintenance candidate first makes it take precedence over normal content; omitting it removes that behavior.
try_files is an existence test, not a replacement for a business rule. Do not use it to infer authorization, user identity or application state. Let the application make those decisions after the request reaches its handler.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
How NGINX processes a rewrite
Server-level processing
Server-context rewrite directives run before NGINX settles on the final location for the current URI. This is why a dedicated server block is a natural place for host canonicalization: the request can be redirected before location-specific routing is considered.
Location-level processing
After a location is selected, its rewrite directives execute in declaration order. A rewrite with last changes the URI and sends NGINX back through location selection. The new location then runs its own applicable directives. A break ends the current rewrite set without that new search, so processing remains with the current location and its handler.
The ten-cycle limit
Every internal redirect or location restart consumes another pass. NGINX limits this rewrite loop to 10 cycles. A configuration that reaches the limit usually has overlapping location patterns, a fallback that rewrites back to its input, or a last rule whose output still matches the same rule. Design an explicit terminal path—an actual file, a named application location or a final response—rather than relying on the cycle limit.
Converting Apache .htaccess rules
NGINX does not process .htaccess files. Its configuration is loaded centrally, so rules must be placed in the appropriate http, server or location context and then the configuration must be tested and reloaded.
| Common Apache intent | NGINX shape | Migration decision |
|---|---|---|
| Host condition that redirects one hostname to another | Separate server blocks with return 301 |
Express the host split structurally instead of reproducing nested conditions. |
| Serve a real file or directory, otherwise route to the application | try_files $uri $uri/ @app; |
Use file checks instead of a chain of negative rewrite conditions. |
| Pattern-based path conversion | rewrite with captures and an intentional flag |
Translate the expression to PCRE syntax and decide whether a new location search is required. |
Apache rules often rely on per-directory context, where the directory prefix has already been removed from the match. NGINX expressions are matched against the request URI in the NGINX location model, so preserve or remove path prefixes deliberately when translating. A direct line-for-line copy is rarely correct. For a large legacy configuration, a dedicated rewrite-rule guide such as the one in Mastering NGINX can be useful, but every translated rule still needs testing in your own location tree.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Practical rewrite recipes
HTTP to HTTPS
server {
listen 80;
server_name example.org www.example.org;
return 301 https://example.org$request_uri;
}
This is appropriate when plain HTTP is never the canonical endpoint. If another device terminates TLS before this server, verify the proxy’s forwarding behavior first; otherwise both layers can disagree about the scheme.
Permanent path move
location = /old-guide {
return 301 /new-guide;
}
A literal path is easier to audit than a regex that matches only one value. Keep the redirect target relative when the host and scheme should remain unchanged.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Pattern-based legacy paths
location /legacy/ {
rewrite ^/legacy/(.*)$ /articles/$1 permanent;
}
The permanent flag makes this a 301. Check that the resulting /articles/ URI is handled by a different location or by the intended application route; if the rewrite must stay within the current location, evaluate break instead of last.
Static assets with an application fallback
location / {
try_files $uri $uri/ @app;
}
location @app {
proxy_pass http://backend;
}
Keep this as the default shape for a front-controller application unless a specific rule genuinely needs regex captures. It prevents a request for an existing asset from being needlessly rewritten into application code.
Validation and troubleshooting
Validate before reloading
- Run the installed NGINX binary’s configuration test, commonly
nginx -t. Fix syntax, context and duplicate-directive errors before touching live workers. - Reload only after the test succeeds, using the service manager or the installed binary’s reload signal. A reload applies the new configuration without treating an untested file as production-ready.
- Exercise both the intended match and a near miss. Test a URI that should redirect, one that should remain internal, an existing file, a missing file and the application fallback.
- Inspect the error log when a request loops, reaches an unexpected location or bypasses a static file. Look specifically for internal-redirection-cycle messages.
Useful request checks
curl -I http://example.org/old-guide
curl -I http://example.org/static/app.css
curl -I http://example.org/missing-route
Header-only requests let you confirm status codes and Location headers without downloading a response body. For an internal rewrite, the client will not receive a redirect location; verify the final status and the server-side handler instead.
Common failure modes
- Unexpected 301 or 302: a replacement beginning with
http://,https://or$scheme, or an explicit redirect flag, turns the rewrite into an external response. Check the replacement and flag. - Rule never matches: the expression may include the wrong directory prefix, appear after a broader rule, or be placed in a location that is never selected. Review the URI being matched and directive order.
- Rewrite loop: a
lastresult is selecting the same location again, or the fallback eventually rewrites to its input. Make the destination terminal or usebreakwhen no new lookup is required. - Static file reaches the application: the
try_filescandidates do not correspond to the on-disk path, or the file test is after an unconditional rewrite. Put existence checks first and verify the server root. - Works directly but not behind a proxy: scheme and host headers may be changed before NGINX sees the request. Reconcile TLS termination and forwarded-header policy before writing canonical redirects.
A decision checklist for production rules
- Is the outcome a single, known response? Use
return. - Does a regular expression need to capture and transform part of the URI? Use
rewrite. - Is the decision based on whether a file or directory exists? Use
try_files. - Should the changed URI be evaluated against another location? Choose
last. - Should the current location keep handling the changed URI? Choose
break. - Are specific expressions above general expressions?
- Does every path have a terminal handler that cannot rewrite back to itself?
- Have you tested the configuration with the installed NGINX binary and checked logs after reload?
The dependable NGINX design is deliberately boring: fixed policies in server-level return statements, narrowly scoped regex rewrites, and try_files for filesystem checks and front-controller fallbacks. Treat last as a request to restart location selection, treat break as a request to stay put, and verify every rule against the actual proxy and TLS topology that serves it.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




