Nginx rewrite - swap path in URL - nginx

Please help me with nginx:
url www.server.com/products/customer/books
should be www.server.com/customer/products/books
So i need to swap /products/ with /customer/

Yes, you can do this with a rewrite. Add the following to your server block:
rewrite ^/customer/products/(.*)$ /products/customer/$1 last;
Or, if you have multiple URLs (ie customer & business), you can do this:
rewrite ^/(customer|business)/products/(.*)$ /products/$1/$2 last;

Related

NGINX Rewrite is encoding querystring as a path

I want to rewrite internally several locations /customers/foo?bar=2 to an existing location in my nginx configuration at /blah so that it's as if the request was to /blah/customers/foo?bar=2.
location /blah {
# View in fiddler
proxy_pass http://127.0.0.1:8888/;
# Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...
Nginx is rewriting the URL with the querystring encoded as a path /blah/customers/foo%34bar=2.
The same thing happens with rewrite ^ /blah$request_uri;. It encodes the ? as %3F effectively garbling the URL.
If I do a client redirect rewrite ^ /blah$request_uri permanent; the URL is correct and contains the ? but I want an internal redirect inside my NGINX config.
Don't use $is_args$args, because the rewrite directive will automatically append any existing query string.
For example:
rewrite ^/customers/(.*) /blah/customers/$1 last;
Although, I would prefer:
rewrite ^(.*)$ /blah$1 last;
Or even:
rewrite ^ /blah$uri last;

nginx rewrite all trailing / to /index.html with proxy pass

Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works

nginx URL rewrites, applying wildcard

I am using nginx to rewrite some urls. how would I take the wildcard part of the initial url and apply it to the destination url?
ex. ^/il-it/news /news where news is its the wildcard and its auto applied to the destination.
rewrite ^/il-it(/.*)?$ / permanent;
Try this -
rewrite ^/il-it/(.+)/?$ /$1 permanent;

nginx location rewrite not matching

In our nginx configuration we have defined the following rewrite to forward requests to another domain:
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
It matches URLs like http://www.example.com/service/test123 but the following URL does not match:
https://www.example.com/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Yes there is a second URL after /ext_css/ and we now that's not correct but at the moment there is no way to change that.
Is there a way to completely forward the whole path to the new server?
/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Doing it outside of the location block did the trick.
Instead of
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
we now have just
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
Now it matches everything written after /service/

Nginx rewrite rule: Add subfolder to URI if not there

Hello I'm trying to write a Nginx rewrite rule that adds a subdirectory to my URL if it's missing. For example, I need http://example.com to be re-written (and redirected) to http://example.com/legacy-app. Can't seem to find the proper example to do this.
You can use a rewrite directive, but an exact match location block is most efficient:
location = / {
return 301 /legacy-app;
}
See this document for more.
We ended up using this:
rewrite ^/$ /legacy-app/ last;

Resources