NGINX equivalent to Apache mod_rewrite's noescape - nginx

I'm trying to migrate a server from Apache to NGINX. It sits in front of an S3 bucket to provide access control and some URL rewriting. I was able to switch everything over except this one weird rewrite rule:
RewriteRule ^([^+]*)\+\+(.*)$ http://s3.amazonaws.com/bucket$1\%2b\%2b$2 [P,NE]
We have a one file that needs to have "++" in the URL. When you request it from S3 the +'s need to be URL escaped. The noescape flag lets you do this in Apache. I tried to do this in NGINX as:
rewrite ^([^+]*)\+\+(.*)$ $1%2b%2b$2 last;
but the percent signs get double escaped and "++" was replaced with "%252b%252b". Is there any way to do this with NGINX?

Try escaping % with another %:
rewrite ^([^+]*)\+\+(.*)$ $1%%2b%%2b$2 last;

Related

nginx rewrite middle of url to another pattern

This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?

Nginx: rewrite to a file without URL changing

I have tried many options, but did not find a suitable answer. I can do this in Apache, but I can’t figure out how to do a redirect in Nginx while maintaining the URL
.../s1 changes to .../image.jpg - how to fix it?
location /s {
rewrite "/s1" https://example.com/image.jpg last;
}
A redirect always changes the url, a rewrite keeps it internally.
In this case nginx is turning your rewrite into a redirect, because you specified a different server. You have 2 options:
If the url you want to rewrite to is local, you should simply remove the https://example.com part to make the url relative instead of absolute.
If you really want to mask a different server, then what you need to build is a reverse proxy. Nginx does have features for this.

How to rewrite path to url with semicolon in nginx?

I am using nginx as a reverse proxy, a Angular Universal application is serving in the background.
I am trying to rewrite an old URL pattern to a new one via nginx, namely
server {
rewrite ^/s/(.+)$ /search\;q=$1 permanent;`
...
}
such that something like /s/keywords gets redirected to /search;q=keywords
Unfortunately, the above nginx rule turns
/s/keywords into /search/;q=keywords (so a slash gets added after /search). Is there a chance to remove this slash so the result is /search;q=keywords?
(The application running behind nginx expects the url to be /search;q=keywords.)
You could add quotes to the replacement, for example:
rewrite ^/s/(.+)$ "/search;q=$1" permanent;

Write a url path parameter to a query string parameter in nginx

I'm trying to rewrite a place name from a url path into a query string on nginx.
I want ourdomain.com/hotels/london?some_key=value to become ourdomain.com?d=london&some_key=value
We were doing it with Apache -
RewriteRule ^hotels/([^/]+)/?\??(.*)$ ?d=$1&$2 [QSA]
And we're currently doing on haProxy (acting as a reverse proxy) -
reqrep ^([^\ :]*)\ /hotels/([^/\ \?]+)/?\??([^\ ]*)(.*)$ \1\ /?d=\2&\3\4
How do I do the same thing on nginx?
Try something like:
rewrite ^/hotels/([^/]+)/?$ /?d=$1 permanent;
The nginx URI always has a leading /. The ? and query string is not part of the normalised URI, but the rewrite directive appends any arguments automatically unless there is a trailing ?.
See this document for details.

Nginx Rewrite Not rewriting the url

I have tried this for hours and can't seem to get it. I copied other examples and still can't get rewrite to work.
I need my url on nginx to look like http://myurl.com/main/login, http://myurl.com/somethigelse/home, est. Any Help Appreciated. I'm new to nginx, seems a lot faster.
My nginx rewrite looks like this:
rewrite ^/([^/]+)/([^/]+)$ /index.php?db=$1&action=$2 last;
rewrite ^/([^/]+)/([^/]+)/$ /index.php?db=$1&action=$2 last;
It works for me. Do you tell nginx to reload the configuration (do a sudo service nginx reload)? Otherwise, nginx will still be using the old config.
Note, that you can use one line by making the final slash optional using a question mark:
rewrite ^/([^/]+)/([^/]+)/?$ /index.php?db=$1&action=$2 last;
# ^

Resources