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;
Related
I want to send a vhost's requests to git.domain1.tld to sub.domain2.tld/git
I suppose that would conflict with overwritten files so how can I get that domain to point to that location?
You can try this:
location / {
proxy_pass https://sub.domain2.tld/git;
}
Or
rewrite ^/$ https://sub.domain2.tld/git permanent;
rewrite ^/(.*)$ https://sub.domain2.tld/git/$1 permanent;
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
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/
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.
I'm breaking my head on Nginx rewrite rules while migrating from Apache to Nginx.
I had .htaccess rewrite rules that made /search/foo+bar/2&pricerange=20-50 to search.php?search=foo+bar&page=2&pricerange=20-50
and I could access all the arguments like you would expect with $_GET['search'], $_GET['page'] and $_GET['pricerange']
But now with Nginx I have issues with this appended arguments like pricerange. Whenever I visit /search/foo+bar/2&pricerange=20-50 it does not translate the pricerange argument.
And when I visit the same url without the page number the pricerange argument gets added to the search argument. But only when I'm using more than one word concatenated with +'s.
My current Nginx rewrite rules:
location / {
try_files $uri /index.php$is_args$args;
rewrite ^/search/(.*)/(.*)/$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/(.*)/?$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/$ /search?search=$1 last;
rewrite ^/search/(.*)/?$ /search?search=$1 last;
}
GET arguments begin after the '?'
/search/foo+bar/2&pricerange=20-50
You need to replace '&' with '?' or rewrite rule
rewrite ^/search/(.*)/([0-9]+)&pricerange=(.*)$ /search?search=$1&page=$2&pricerange=$3 last;