Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

If an Nginx location block is not redirecting, first prove which server block and location handled the request. For a straightforward client-visible redirect, use an explicit return in the matching server or location context, then validate and reload the active configuration. The usual causes are location precedence, a URI pattern that does not match the request, a different server block, an internal redirect from try_files or error_page, or an upstream application sending a different response.

The quickest reliable fix

For one known path, start with an exact match and return:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
server {
    listen 80;
    server_name example.com;

    location = /old {
        return 301 https://example.com/new;
    }
}

The = modifier makes this an exact URI match. The return directive immediately sends a client-visible response; it does not ask Nginx to find another local handler for the old URI. A permanent redirect uses status 301. Change the destination and hostname to your actual values.

After editing, test the configuration and reload the same Nginx installation that serves the request:

#1 Best Overall
Sale
Compressed Air Duster,130000RPM Super Power Cordless Air Duster,Rechargeable Brushless Blower,3 Gear Adjustable Blower with LED Light,Dust Remover for Computer/House/Outdoor/Car
  • Powerful Turbo Fan:CAGIWIRU Electric Air Duster have reached speed up to 130000RPM,Can efficient remove dust and debris.With LED light and Three Adjustable Speed to meet with different cleaning tasks.
  • With Attached USB C Fasting Charging cable.Charing 3 hours,Enjoy up to 240 minutes of use on the lowest setting, For third speed can use for 25mins.with four charging options to meet with your needs.
  • Wide Application:Upgraded attached 5 different nozzles,making it suitable for a variety of scenes, such as car,pet, pc, keyboards, and other electronic devices. It also serves for office and home clean duster.
  • Handy Design & Portable:Cordless air duster electric nozzle is Light weight and small size, design and comfortable to hold and space saving, convenient to carry around. You can put it in your room or inside the car without taking up space.
  • Warranty & Support: Our Electric air duster comes with 5 years warranty and 24/7 customer service. During this period, if you have any questions,pls kindly feel free to contact us.
sudo nginx -t
sudo nginx -s reload

Then inspect the response rather than relying on a browser cache:

curl -I http://example.com/old
curl -v http://example.com/old

Confirm the status, the Location header, the Host you sent, the scheme, and whether the request URI includes or omits a trailing slash. If the response is not the one above, continue through the location-selection and logging checks below.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why Nginx appears to ignore a location block

The request may be reaching another server block

Nginx chooses a server block using the listening address and port and the request’s host name. A correct location in a file that is not included, or in a server block for another host or port, cannot affect the request. A request sent to an IP address, an unexpected port, or a different Host header can therefore appear to bypass a perfectly valid redirect.

Do not infer the active configuration from the file you edited. Use the expanded configuration output described in the diagnostic procedure, and verify the exact server_name, listen directive, and included file that should receive the request.

Location matching has a defined order

Nginx tests the request URI against all applicable location directives and applies the directives in the location it selects. The practical precedence is:

  1. An exact location such as location = /old is selected immediately.
  2. If there is no exact match, Nginx retains the longest matching prefix location, such as location /old/.
  3. Regular-expression locations are then evaluated in their configuration order. A matching regular expression can replace the retained prefix.
  4. A longest prefix marked ^~, for example location ^~ /old/, prevents regular-expression locations from overriding that prefix.

This means that adding a broad regex can change the result of a prefix block that previously worked. Conversely, adding ^~ can stop a regex redirect from ever being considered. Exact matches are useful when one path must win regardless of broader rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Match the actual URI, not the URL you have in mind

Location matching uses the request URI; query-string arguments do not determine which location matches. A rule for /old does not match /old/, and a rule for /old/ does not match the slashless form as an exact match. Case also matters for ordinary URI comparisons. Capture the request exactly with curl -v, including its trailing slash, and compare it character by character with every candidate location.

Choose return or rewrite

Need Preferred directive What happens
A simple client-visible redirect return Sends the chosen HTTP status and Location immediately; it is the clearest option.
Conditional matching with captures or a rewrite rule rewrite Transforms a URI according to a pattern and flags; it may trigger another location search.
Internal routing without telling the browser to change URL rewrite with deliberate flow control Processes another URI inside Nginx; the client may continue to display the original URL.
One old path to one new URL return 301 or a temporary return status Produces an explicit external redirect that is easy to inspect with curl.

Use return 301 https://example.com/new; when the move is permanent. Use a temporary redirect status while testing or when the destination can change; choose the status deliberately rather than changing a working rule later after clients have cached a permanent response. A temporary redirect can be represented with 302 or 307, depending on the behavior your application requires.

Rank #2
EVEO® Screen Cleaner Spray Kit (Streak-Free) TV Screen Cleaner for Smart TV, Lcd, LEd, Oled | Computer Screen Cleaner Spray and Wipe Microfiber Cloth - TV, Monitor, Laptop, iPad, Car - 6.7oz 1 Pack
  • Screen Cleaner is Your Screen’s New Bestfriend! - EVEO is proud to present a screen cleaner that’s perfectly suited for your TV. Meet your new screen cleaner, computer screen cleaner, laptop cleaner, iPad cleaner, Macbook,tv cleaner or any other electronic device. Our screen cleaner spray and wipe along with the included plush microfiber cloth, easily removes dust, fingerprints & other smudges on the screen’s surface. Clean effectively for a streak-free screen
  • Gentle on your Screens - Our screen cleaner spray and wipes is compatible to be used even for the most sensitive LCD, LED, CRT, and OLED screens in proper use. Be at ease knowing that this product is compatible with all the major brands: LG, SONY, Samsung, Sharp, iPad, iPhone, Android screens, Kindle, PC monitors - we’ve got you covered! Can be used as LCD screen cleaner, tv cleaner, smart tv screen cleaner, and more
  • Premium Super Soft Plush Microfiber Cloth - Included in this efficient screen cleaning kit. This screen cleaner spray and wipe has our signature plush microfiber cloth specially designed to comply with screens and monitors and leave them streak-free. Has been shown to effectively absorbs stubborn stains, zero streaks, and no fiber shedding. Always use this together with our screen cleaner for the best results
  • Easy-to-Use Screen Cleaner Kit + microfiber cleaning cloth - The EVEO Screen Cleaning Kit will easily be your new favorite tool when it comes to taking care of the things you love most, quick and easy solution for a streak-free screen. Simply screw on the sprayer gun on the screen cleaner bottle. Spray the cleaning liquid onto the microfiber cloth, then wipe the screen until desired cleanliness & shine is achieved.
  • Quality Screen Care Kit Worth Purchasing - Enjoy a convenient Screen Cleaner anytime, anywhere you need it. Your satisfaction is our top priority. We stand behind the quality of our products and that’s why, you shouldn’t be worrying at all. We manufacture the Best Screen Cleaner. and can be used for laptop cleaning kit, laptop screen cleaner, tv screen cleaner for smart tv. MacBook screen cleaner, monitor cleaner or screen cleaner spray for electronic devices with microfiber cleaning cloth

Because return is unconditional within the context where it runs, put it in the narrowest location that should redirect. A return in the server context affects every request handled by that server, while a return in an exact or prefix location limits the scope.

Build the location pattern that matches your requirement

One exact path

location = /old {
    return 301 https://example.com/new;
}

This handles only /old. Requests for /old/, /old/page, and other paths require separate rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A directory or path prefix

location /old/ {
    return 301 https://example.com/new/;
}

This prefix can cover paths below /old/. Check for competing regular expressions, because a regex location can supersede a prefix unless the prefix is marked ^~.

A prefix that must beat regex locations

location ^~ /old/ {
    return 301 https://example.com/new/;
}

Use ^~ only when this prefix should prevent regex locations from taking over. It is a precedence choice, not a general fix for a malformed pattern.

A regular expression

location ~ ^/legacy/(.*)$ {
    rewrite ^/legacy/(.*)$ /new/$1 last;
}

Regex rules are useful when a capture is genuinely required. Keep the expression as narrow as possible, and inspect the other regex locations in their order because the first matching regex can determine the result.

Understand rewrite flags before using them

A rewrite can alter the URI that Nginx processes next. The flag controls whether Nginx searches for a new location:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • last ends the current rewrite set and starts a new location lookup using the rewritten URI. This is appropriate when the new URI should be handled by another location.
  • break stops the current rewrite processing but keeps the request in the current location. Use it when the remaining directives in that location should handle the rewritten URI.

For example, this intentionally rematches after changing the path:

location /legacy/ {
    rewrite ^/legacy/(.*)$ /new/$1 last;
}

location /new/ {
    # The new URI is handled here.
}

Using last when the rewritten URI still matches the same rule can create an internal redirect cycle. A cycle may end in an error instead of the redirect you expected. Before choosing last, identify the location that should handle the resulting URI and ensure the new path cannot repeatedly re-enter the same rewrite. Use break when rematching would be wrong.

Rank #3
BladeHawks RGB Gaming Mouse Pad Extra Large Extended 31.5x12 Inch-Black
  • 【Large size】:Measuring 800 x 300 mm/ 31. 5×12 inches, which is wider and taller than others. Large mouse pad can perfectly fit your keyboard and mouse of any size, and there is also ample space for peripherals such as phones, tablets, etc.
  • 【LED backlight mouse pad】:Blade Hawks RGB mouse pad provides 7 static modes and 7 dynamic modes will give you the options you are looking for. Power-off memory function remembers the last lighting mode you selected. The super glow fiber Chroma will give you a colorful gaming setup that you want.
  • 【Anti-slip rubber base】:Made from Natural rubber, this gaming mousepad will firmly grip your desktop, allowing you to enjoy the ultimate precision in the games you are most passionate about. It also comes with a texture that enhances this ability even further.
  • 【Smooth and waterproof surface】: Micro-textured cloth surface, for extremely precise mouse control and a smooth movement. When liquid splashes on the gaming mouse pad, it forms water droplets and slides down. Will not delay your work or gameplay.
  • 【Plug and play】: This mouse and keyboard pad is powered by USB, plug and play, no driver required. One button control light modes. Press one time to change the lighting mode, press twice to change the brightness, press and hold about 3 seconds turn ON/OFF.

If the reader must see a different URL, prefer an explicit return. An internal rewrite is not a substitute for a client-visible redirect: it can leave the browser showing the original address while Nginx serves another resource.

Why try_files and other directives change the result

try_files can perform an internal redirect. Instead of returning the response you expected from the original location, it may select another URI and location. For example, a fallback such as try_files $uri $uri/ /index.php; can send processing to the application entry point rather than to a redirect rule you thought would run first.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a response has an unexpected status or Location header, inspect the whole request path for these handoffs:

  • try_files fallbacks that name another URI;
  • index processing that selects a directory index;
  • error_page directives that internally route an error;
  • a proxy or FastCGI upstream returning its own redirect;
  • application code issuing a redirect after Nginx has selected the expected location.

The header and status tell you where to look. A redirect generated by an upstream application can look like an Nginx location failure unless you compare the response while watching both access and error logs.

A complete diagnostic sequence

1. Dump the active, expanded configuration

Run:

sudo nginx -T

Use appropriate privileges for your installation. The command prints the complete configuration after includes are expanded. Search the output for the intended server_name, listening port, every competing location, regex modifiers, try_files, error_page, and any rewrite or return directives. This often reveals that the edited file is not included, a second server block wins, or a broader rule appears earlier than expected.

2. Confirm which Nginx instance and paths are active

Run:

nginx -V

This exposes the compiled paths and build options. Use those paths when locating the configuration and log files, and make sure the reload and test commands address the same Nginx instance that owns the listening socket. Testing one installation while another process serves traffic produces misleading results.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Compare the request URI with each location

Check the host, port, scheme, path, trailing slash, and case. Remember that query arguments do not select a location. Test both slash forms when the rule is intended to cover both, and test a representative child path for a prefix rule.

curl -v http://example.com/old
curl -v http://example.com/old/
curl -v 'http://example.com/old?x=1'

The verbose output records the request line and host header, so you can compare what was actually sent with the configuration.

Rank #4
Sale
Vaydeer Wrist Rest for Keyboard and Mouse, Computer Ergonomic Wrist Support Pad, Soft Memory Foam Arm Cushion for Desk, Palm Hand Office Laptop Typing
  • 【Softer and More Comfortable】Vaydeer wrist rest has unique diamond pattern, which is the combination of softness and aesthetics. The materials of wrist rest are improved into higher quality memory foam and covered with silky smooth lycra. The computer wrist rest makes you as comfortable and cushiony as like rest your wrists on clouds.
  • 【Ergonomic Wrist Saver】The wrist rests for keyboard and mouse comes with a 17.32×3.15×0.83 inch keyboard wrist pad and a 5.94×3.15×0.83 inch mouse wrist support. Based on ergonomic design, the unique concave shape is the perfect fit for your wrist joints. The wrist rest pad fits most computer keyboards and laptops, improve hand and wrist posture, release your wrist and arm stress.
  • 【Non-Slip Rubber Bottom】Featuring an anti-skid silicone base on the bottom, this wrist keyboard support stays firmly in place on your desk, preventing the padding from sliding around, ensuring stable and consistent wrist support during extended computer sessions.
  • 【Better Experience & Pain Relief】Our keyboard arm rest is beneficial to alleviate the soreness caused by direct contact and friction between your arm and a hard desk surface, reducing the risk of wrist fatigue or carpal tunnel. The soft texture of memory foam can evenly distribute the pressure around your wrists and provide good support with just enough give.
  • 【Helpful in Multiple Scenarios】Whether you're working, studying, writing, typing, gaming, this keyboard and mouse rest combo is an essential accessory to add comfort and support to your hands and wrists. It’s also a great gift for men, women, family, friend, coworker, gamer, teacher, etc.

4. Reduce a simple redirect to an explicit return

Temporarily or permanently replace a complicated rewrite with an exact return in the intended location. If that works, the server and location are correct and the earlier problem is in rewrite flow, a competing regex, or a downstream directive. If it does not work, continue checking server selection and the active configuration instead of adding more rewrite rules.

5. Validate before reloading

sudo nginx -t

Do not reload a configuration that fails syntax validation. A successful test confirms syntax for the configuration Nginx is reading; it does not prove that the request reaches the location you intended, so perform the curl test afterward.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

6. Inspect status and headers directly

Use curl -I for headers and curl -v when you need the complete exchange. Record the HTTP status and Location value, along with the host, scheme, and slash form. If there is no Location header, the response may be an internal rewrite, an upstream response, or a non-redirecting handler. If the destination is unexpected, inspect the rule or application that supplied that header.

7. Watch access and error logs during one request

Tail the configured logs while issuing a single request. Access logs show the request and resulting status; error logs expose rewrite cycles, configuration problems, and internal processing failures. The exact file paths vary by build, so use the active configuration and paths reported by nginx -V and nginx -T.

An error_log directive inside a server or location overrides inherited logging settings. If you are watching a parent-level file while the selected location defines another error_log, you may be looking in the wrong place.

8. Increase detail only for controlled troubleshooting

If the build supports it and normal logs are insufficient, temporarily raise the error_log level or enable debug logging for a controlled client address. Reproduce one request, capture the relevant entries, and restore the normal level afterward. Broad, long-running debug logging can generate excessive output and obscure the request you are investigating.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common failure modes and targeted fixes

The rule matches /old, but the request is /old/

An exact match is literal. Add a deliberate rule for the slash form, use an appropriate prefix, or decide which canonical form should redirect to the other. Test both forms after reloading.

A regex defeats the prefix redirect

Inspect regex locations in configuration order. If the prefix must always win, use the ^~ modifier; if the regex is the intended handler, move or narrow the rule and make its scope explicit.

Best Value
Sale
Under Desk Cable Management Tray No Drill, Metal Mesh Cable Management Under Desk with Clamp Mount, 2 Hole Cable Tray with Wire Management and Cord Organizer for Home Office Standing Desk
  • 【No Drill Mounted】Cable management tray can be assembled in as little as 3 minutes or less-simply. Side clips easily clampe the edge of desk, pass through the holes on the sides, and secure excess cables with baskets. Desk cable management supports inward or outward installation to meet your needs in different scenarios, and provides you with great convenience when collecting and organizing wires.
  • 【Rubber Pad Protects Desk】Wire organizer under desk built-in soft rubber pads on the clamp protect your desk from scratches, so you will not damage your furniture or office.
  • 【Sturdy and Beautiful】Cord management under desk is made of high-quality carbon steel and holds about 15 lbs. The wire basket has 2 holes on each side of the tray to enable your cables to pass freely. Cable baskets are suitable for desk thickness use from 0.4" to 2.4".
  • 【Space Saving & Keep Organized】Desk cable management tray holds all your power cords, outlet strips, USB hubs and computer transformers hidden from your feet, and the data cables will not enter the vacuum cleaner to keep your desk clean and tidy. Desk wire organizer is also suitable for use in the kitchen and outdoors.
  • 【Reduce Safety Risks】Cable tray under desk keeps all plugs away from your kids, no more worry about tripping. Using 2 holes wire tray saves extra space, helps you easily cabling and wire protection. The mesh design is not easy to accumulate dust, but avoids safety accidents caused by messy wires.

The edit is in the wrong server block

Compare the request’s host and port with the selected server_name and listen directives in nginx -T. Requests sent to a default server, an IP address, or another port will not use a location from an unrelated virtual host.

The browser shows an old redirect

Test with curl to remove browser behavior from the diagnosis. A 301 communicates a permanent move, so use a temporary status while experimenting if the destination is not final. Verify the current server response before changing application code.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The response is from an application or proxy

Inspect the Location header and logs. If Nginx selected the expected location but the upstream supplied a redirect, fix the upstream rule or its understanding of host and scheme. A proxy response is not corrected by adding another unrelated location block.

A rewrite loops or ends with an internal redirect error

Trace the URI after every rewrite. With last, Nginx performs a fresh location lookup; if the new URI matches the same rewrite, the cycle repeats. Change the target so it exits the rule, or use break when processing should remain in the current location.

try_files sends the request to a fallback

Read the complete try_files sequence and identify its final URI. That internal redirect may be the path that eventually produces the response. Place the redirect where the selected flow can reach it, or remove the fallback only if it is not required by the application.

Choosing redirect status and scope

Situation Choice Reason
The old URL has permanently moved return 301 Communicates a permanent client-visible redirect.
You are testing or the destination may change return 302 Keeps the redirect temporary while you verify behavior.
A temporary redirect must preserve the request method return 307 Use when method-preserving temporary semantics matter to the client.
Only one URI should move Exact location = Prevents unrelated paths from inheriting the redirect.
An entire path tree should move Prefix location Covers descendants, subject to regex precedence.

Keep the redirect’s scope and status aligned. A broad permanent return in the server context can unexpectedly move every path, while a narrow exact rule can leave child URLs untouched.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Final verification checklist

  • nginx -T shows the edited file, the intended server block, and the competing locations.
  • The request host, port, path, case, and trailing slash match the rule you selected.
  • Exact, prefix, ^~, and regex precedence produce the location you expect.
  • The redirect uses return when the browser must receive a new URL.
  • Any rewrite uses last or break intentionally and cannot re-enter itself indefinitely.
  • try_files, index, error_page, proxy responses, and application redirects have been checked.
  • nginx -t succeeds before reload, and the reload targets the active Nginx instance.
  • curl -I or curl -v confirms the expected status and Location header.
  • Access and error logs for the selected server or location show the request you just made.

The Bottom Line

Most failed Nginx redirects are fixed by identifying the active server and winning location, then replacing unnecessary rewrite logic with an exact return. Validate with nginx -t, reload the correct instance, and confirm the status and Location header with curl while watching the selected logs.

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.