Nginx, URL rewrite: Location or $request? - nginx

I need some help to understand the Nginx rewrite function.
Imagine, a real file is at the following address:
http://www.domain.com/library/content/folder/country/page/index.html
I want to rewrite the URL, to have something better (more human readable!):
http://www.domain.com/page
1. How can I do that with Nginx?
location = /... {
rewrite ...
}
Or
if ($request ~* "page") {
rewrite ...
}
2. How can I write this rule ?
If I was using Apache, I will write something like this:
RewriteRule ^page /(.*) library/contents/folder/contry/page/$1 [L]

You can use the alias or root instructions in a location block
location /page/ {
root /my/absolute/path/library/content/folder/country/;
}
location /page/ {
alias /my/absolute/path/library/content/folder/country/page/;
}
Source: http://wiki.nginx.org/HttpCoreModule

How about
This problem is related to versioning of API based on header
INPUT url : http://abcd.com/v1/getshafts?param=q ,header = v1.1
OUTPUT : abcd.com/v1.1/getshafts/
what should be 'rewrite' value here?

Related

NGINX rewrite args without an IF?

I would like to rewrite legacy links using a query parameter type of URL to a new style of URL.
Ex.
example.com/page?id=1 -> example.com/page/1
example.com/otherpage?id=1 -> example.com/otherpage/1
Currently I have the following configuration using the evil if.
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite .* $scheme://$host/page/$w1? permanent;
}
Note: I am using CloudFront, and relying on the host header above.
If the above is in a server block, with no other location block - would this qualify as a non-evil use of if in NGINX config? Also, the above only supported /page/. Any better ideas for making that portion work for otherpage and other pages?
I have seen a few other ideas discussing using a map, but I'm not quite sure how to bring it all together? I was thinking something along the lines of:
map $args_id ?? {
default ?
??
}
...
server {
...
???
}
UPDATE:
Based on the Answer from #Ivan, this was my final solution:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
# Handle legacy requests
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite ^ $scheme://$host$uri/$w1? permanent;
}
}
Your if construction isn't evil. You can use something like
rewrite ^ $scheme://$host$uri/$w1? permanent;
for any page. More complex example if you want to process both example.com/page?id=1 and example.com/page/?id=1:
map $uri $maybe_slash {
~/$ "";
default "/";
}
...
server {
...
rewrite ^ $scheme://$host$uri$maybe_slash$w1? permanent;
...
}

nginx rewrite rule with query parameter

My URL looks like:
localhost/video-detail?videoID=T0r-uCXvDzQ
I want to serve a page with name: T0r-uCXvDzQ.html (videoID.html) which is present in server's file system.
I am trying to write the rewrite rule as follows:-
location / {
rewrite ^/video-detail?videoID=(.*) /$1.html;
}
Also tried:
location / {
rewrite ^/video-detail?videoID=(.*) /$arg_videoID.html;
}
But they are giving 404 error.
How can I use the query parameters in the output rewrite rule.
The following worked for me:-
if ($args ~* "videoID=(.*)") {
set $key1 $1;
rewrite ^(/video-detail)$ /$key1.html;
}

Rewrite rule for nginx?

I'm trying to deploy trac with nginx. I almost have everything working exept for the rewrite rule for serving static files. I need to rewrite this url:
http://trac.domain.tldn/chrome/common/feed.png
to this one:
http://trac.domain.tldn/static/htdocs/common/feed.png
I have this code, but it isn't working:
location ~ /(.*?)/chrome/common/ {
rewrite /(.*?)/chrome/common/(.*) /$1/static/htdocs/common/$2 break;
root /var/www/domain.tldn/static/trac/static/htdocs/common;
}
Can you help me with this?
You can do it with the following code:
location /chrome/common {
rewrite ^/chrome/common/(.*) /static/htdocs/common/$1 permanent;
}
Or just use an alias for your files path:
location /chrome/common {
alias /var/www/domain.tldn/static/trac/static/htdocs/common;
}

Nginx - Redirect url for certain file type

I have nginx 1.2.1. I want to rewrite:
http://mywebsite.com/[token].mp4 to http://mywebsite.com/get.php?token=[token]
Return error 404, my block:
location ~* \.(mp4)$ {
rewrite "^/([a-zA-Z0-9]{23})\$" /get.php?token=$1 break;
}
I tried this question but nothing, it returns error 404
According to nginx.conf you provided here, try below:
location ^~ /videos/ {
rewrite "^/videos/([a-zA-Z0-9]{23})\.mp4$" /get.php?token=$1 break;
}
This should match URL: example.com/videos/abc01234567890123456789.mp4 and redirect to example.com/get.php?token=abc01234567890123456789
DISCLAIMER: config not tested, may have some typos
Perhaps try looking at the headers specifically content type like so:
https://unmitigatedrisk.com/?p=359

Rewrite nginx cut last location

I would like to rewrite the following URL http://example.net/bar/foo to http://example.net/bar.
Do you know how to do this ?
Use this :
location /bar {
rewrite ^/bar/(.*)/[^/]+$ /bar/$1;
}

Resources