From ea435049b3a3f5057b3a894040df3cf4f3256d9e Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 23 Apr 2023 22:33:49 +0200 Subject: Refactored syntax highlighting shortcodes into markdown --- content/blog/miscellaneous/nginx-rewrite-break-last.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'content/blog/miscellaneous/nginx-rewrite-break-last.md') diff --git a/content/blog/miscellaneous/nginx-rewrite-break-last.md b/content/blog/miscellaneous/nginx-rewrite-break-last.md index 7cb854e..6cc435e 100644 --- a/content/blog/miscellaneous/nginx-rewrite-break-last.md +++ b/content/blog/miscellaneous/nginx-rewrite-break-last.md @@ -13,7 +13,7 @@ Today I was called in escalation to debug why a set of rewrites was suddenly mis ## Outside a location block When used outside a location block, these keywords stop the rules evaluation and then evaluate to a location. Consider the following example : -{{< highlight nginx >}} +```nginx server { [...] location / { @@ -28,11 +28,11 @@ server { rewrite ([^/]+\.txt)$ /texts/$1 last; rewrite ([^/]+\.cfg)$ /configs/$1 break; } -{{< /highlight >}} +``` If you run several curls you can see the behaviour illustrated : -{{< highlight sh >}} +```sh curl http://localhost/test root # we hit the root handler without any redirect matching @@ -41,14 +41,14 @@ texts # we hit the rewrite to /texts/test.txt, which is then reevaluated and hi curl http://localhost/test.cfg configs # we hit the rewrite to /configs/test.cfg, which is then reevaluated and hits the configs location -{{< /highlight >}} +``` ## Inside a location block When used inside a location block a rewrite rule flagged last will eventually trigger a location change (it is reevaluated based on the new url) but this does not happen when break is used. Consider the following example : -{{< highlight nginx >}} +```nginx server { [...] location / { @@ -63,11 +63,11 @@ server { return 200 'configs'; } } -{{< /highlight >}} +``` If you run several curls you can see the behaviour illustrated : -{{< highlight sh >}} +```sh curl http://localhost/test root # we hit the root handler without any redirect matching @@ -76,7 +76,7 @@ texts # we hit the rewrite to /texts/test.txt, which is then reevaluated and hi curl http://localhost/test.cfg 404 NOT FOUND # or maybe a file if you had a test.cfg file in your root directory! -{{< /highlight >}} +``` Can you see what happened for the last test? The break statement in a location stops all evaluation, and do not reevaluate the resulting path in any location. Nginx therefore tries to serve a file from the root directory specified for the server. That is the reason we do not get either `root` or `configs` as outputs. -- cgit v1.2.3