Meepo & Nginx
Hey Nginx, ever thought about a sneaky redirect that turns a regular request into a surprise little game? I've got a trick that might just make you double‑check your rewrite rules.
Sure, but first make sure the request actually hits the rewrite block and not a preceding location that serves a static file. If the regex is too greedy you’ll end up in a loop or a 500. Also remember that rewrite directives run before try_files, so if you want to preserve the original URI for a fallback, you need to capture it properly. Got a sample config? I can point out the subtle trap.
Sure thing, here’s a quick snip that keeps the rewrite happy and avoids the loop trap:
```
server {
listen 80;
server_name example.com;
# Make sure the request lands here, not in a static block
location / {
# Capture the original URI for later use
set $orig_uri $uri;
# Rewrite to /newpath only if it doesn’t already start with /new
rewrite ^/(?!new)(.*)$ /new/$1 break;
# If the rewrite didn’t happen, fall back to the original
try_files $orig_uri $uri =404;
}
# Serve the “new” content from a different root
location /new/ {
root /var/www/new;
try_files $uri $uri/ =404;
}
# Static files block – this runs last, so rewrites won’t hit it
location ~* \.(jpg|png|css|js)$ {
root /var/www/static;
expires 1d;
}
}
```
A few quick notes: the `break` keeps the rewrite from re‑entering the location block, the `(?!new)` avoids turning `/new/...` into `/new/new/...`, and the `$orig_uri` catch keeps the original URI intact for the fallback. Give it a spin and let me know if any sneaky edge cases crop up!
Sounds solid—just double‑check that the regex won’t swallow a leading slash when you capture `$1`; otherwise `/foo` becomes `/new//foo` and the downstream location might misinterpret it. Also, if you ever add sub‑domains, remember to keep the `server_name` in sync; otherwise the rewrite won’t fire. Happy tuning!
Thanks for the heads‑up, I’ll keep an eye on that double slash and the server_name dance. No worries—got to keep the tricks tight and the tunnels clean!
Glad to help—just remember the subtlety of that slash, it’s the devil in the detail. Keep the config lean, and the server will thank you for the clean tunnels. Happy hacking!
No worries, I’ll keep it slick—no extra slashes, no extra fuss. Let’s keep the tunnels smooth and the bugs at bay. Happy hacking!