nginx location rewrite not matching - nginx

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/

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 url to remove subdirectory

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;
}
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

Removing path from urls in Nginx

My site works correctly as
https://example.com/website/page/Home
https://example.com/website/page/AboutUs
I would like to remove the /website/page part so that end users would see https://example.com/Home etc.
I read on Nginx website that rewrites are not preferred so I tried:
Location / {
try_files $uri /website/page$uri;
}
I get an internal serval error. What is the right way?
Consider using a rewrite like:
# rewrite example.com/awesome to example.com/website/page/awesome
rewrite ^(/.*) /website/page$1 break;
This should work similarly:
rewrite ^/(.*) /website/page/$1 break;

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

Nginx rewrite to proxy_pass server/path

Is it possible to use Nginx proxy_pass to rewrite URL as below:
location /foo {
proxy_pass http://external-server-IP:8080/some/path/;
}
Just in case someone still needs this, the easy way to do this:
location ~ ^/foo/.* {
rewrite ^/foo(.*) /$1 break;
proxy_pass https://external-server:8080/remote-path/;
}
rewrite ^/foo$ /foo/ redirect;
What this does is that it sends the request to the external server, masquerading it under your own domain.
rewrite ^/foo(.*) /$1 break;
The first rewrite is just to remove the added URL path (the remote server is not expecting that.
rewrite ^/foo$ /foo/ redirect;
And the 2nd rewrite is just in case you want to use the index page, so that it actually goes to the remote index page as well.

Resources