0

I would like to rewrite an URL to remove recurring parts from the URL by nginx. Is that possible?

example: dummy.com/sub1/sub2/myproject/trunk --> dummy.com/myproject/trunk

Is a nginx rewrite rule the right way to solve this problem?

location /sub1/sub2 {
  rewrite ^/sub1/sub2/(.*)$ $1 last;
}
userk23r
  • 1
  • 2
  • Yes, it is. The only thing, every URI should start with a slash, so the right rule is `rewrite ^/sub1/sub2/(.*)$ /$1 last;` – Ivan Shatsky Oct 27 '20 at 15:52
  • Hmm this approach covers the tutorials I found concerning this problem. But it does not work. Part of the location is also a proxy_pass, maybe that's the problem? I try to access a SVN-repo on a server by proxy_pass. The path of the SVN is 123.456.789.0:81/svn/repos/$myprojects. I'd like to eliminate the /svn/repos/ in the url. – userk23r Oct 28 '20 at 12:10
  • If you don't want nginx to search a new location after rewriting an URI you should use `rewrite ... break;` instead of `rewrite ... last;` (check the [documentation](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)). But if you want to stip some URI prefix before passing the request to some backend via `proxy_pass`, you have a much better choice: `location /svn/repos/ { ... proxy_pass http://123.456.789.0:81/$myprojects; }`. Check [this](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx) Q/A for details. – Ivan Shatsky Oct 28 '20 at 16:58
  • Or better add your full `location` block to your question. – Ivan Shatsky Oct 28 '20 at 16:58
  • @IvanShatsky thanks for your suggestions. I found a solution by putting the proxy_pass with the subfolders in root location. servername dummy.com location / { proxy_pass http://123.456.789.0:81/sub1/sub2/; } – userk23r Oct 29 '20 at 10:48

1 Answers1

0

Solution was to put a proxy_pass in location /

servername dummy.com
location / {
proxy_pass 123.456.789.0:81/sub1/sub2/; 
}

A rewrite was not necessary

userk23r
  • 1
  • 2