0

Below config works with -

  1. https://example.com/blog/blog/blog-slug

  2. https://example.com/blog/blog-slug

I want to make it work only with the second option

https://example.com/blog/blog-slug

location /blog/ {
    rewrite ^/blog(/?.*)$ $1 break;
    proxy_pass http://1.10.4.204/;
}

Please guide me on how to fix it.

Nilesh Mistry
  • 339
  • 2
  • 13
  • Use [negative lookahead assertion](https://www.regular-expressions.info/lookaround.html): `rewrite ^/blog(/(?!blog/).*) $1 break;` – Ivan Shatsky Oct 16 '21 at 08:53
  • Not working the same issue. – Nilesh Mistry Oct 18 '21 at 09:06
  • do I need to make changes on location level? location /blog/ – Nilesh Mistry Oct 18 '21 at 09:14
  • Indeed, you faced two different nginx mechanisms overlap. An interesting one! My `rewrite` rule works as expected, transforming `/blog/blog-slug` URI to `/blog-slug` but leaves `/blog/blog/blog-slug` URI untouched. However since you are using `proxy_pass` directive with a trailing slash, `/blog/` URI prefix, if still being present after the rewrite, will be stripped off and replaced with a single slash due to the `/blog/` argument of `location` directive, see [this](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx) SO thread for details. – Ivan Shatsky Oct 18 '21 at 10:58
  • For some more detailed explanation see [NginX trailing slash in proxy pass url](https://stackoverflow.com/questions/22759345/nginx-trailing-slash-in-proxy-pass-url) SO thread. The above means that generally you don't need to use that `rewrite ^/blog(/?.*)$ $1 break;` directive at all, using a prefix matching location and a trailing slash for the `proxy_pass` backend argument, `/blog` prefix can be automatically stripped from the proxied URI without an additional `rewrite` rule. Those `rewrite` rules usually used to transform original URIs in the regex matching locations. – Ivan Shatsky Oct 18 '21 at 10:59
  • For the detailed expanation on `proxy_pass` directive behavior and the rules it follow to choose an URI to pass for the backend, see [this](https://stackoverflow.com/questions/54084239/proxy-pass-overwrites-the-url-changed-by-rewrite-directive) SO thread. Now back to the question. Since you want to process `/blog/blog-slug` and `/blog/blog/blog-slug` URIs differently, final answer will depend on how do you want to process a `/blog/blog/blog-slug` request. Do you want to pass it to your `http://1.10.4.204` backend unchanged or should it be processed by some other `location` block? – Ivan Shatsky Oct 18 '21 at 10:59
  • thanks, @IvanShatsky backend will be unchanged. I want to work only with /blog – Nilesh Mistry Oct 18 '21 at 12:05
  • Sorry, still don't understand. Should `/blog/blog/blog-slug` URI be passed to the same backend untouched (unlike `/blog/blog-slug` URI that should be passed as `/blog-slug`) or it shouldn't be processed by this `location` block at all? – Ivan Shatsky Oct 18 '21 at 12:09
  • Nginx passes as it is to the apache server. I have 2 separate machines one for Nginx and the second is apache where Wordpress is there. Currently, Nginx URI passes as it is to the apache server but my WordPress needs to work only with /blog, not to the /blog/blog/ – Nilesh Mistry Oct 18 '21 at 13:24
  • Actually, when the user goes /blog/blog it should say not found 404. that is not happening. – Nilesh Mistry Oct 18 '21 at 13:26
  • Try `location ~ ^/blog/(?!blog/) { rewrite ^/blog(/.*) $1 break; proxy_pass http://1.10.4.204; }`. Note the absence of the trailing slash on the backend name! If you don't need to transform a `/blog/blog-slug` URI to `/blog-slug`, remove that `rewrite` directive. – Ivan Shatsky Oct 18 '21 at 13:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238324/discussion-between-nilesh-mistry-and-ivan-shatsky). – Nilesh Mistry Oct 19 '21 at 13:30

0 Answers0