Rewrite rule for nginx? - 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;
}

Related

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

What is the syntax for a nginx url rewrite

I have an app running at
http://localhost/subfolder/myapp/
How do I create a rewrite rule so that the following urls get redirected to my app?
http://localhost/subfolder/myapp/route1
http://localhost/subfolder/myapp/route2
http://localhost/subfolder/myapp/routeN
...anyother routes after http://localhost/subfolder/myapp/
add this block nginx rules
location ~ /subfolder/myapp/.* {
redirect from here
}
In Nginx you can do rewrites with a return inside your server block:
location ~ /subfolder/myapp/route(.*) {
return 301 http://localhost/subfolder/myapp;
}
This seems to work:
rewrite ^/subfolder/myapp/.+$ /subfolder/myapp last;

How to write this nginx rewrite rule

I have a nginx rewrite rule like this
location ~* /question\-(.*)\.html$ {
rewrite "^/question-([0-9]+).html$" "/question/$1.html";
rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
}
This rule mean is:
if URI is /question-123456.html so rewrite to /question/123456.html
/question/123456.html is static file, and via question.php to create.
So when I visit http://example.com/question-123456.html HTTP rewrite to http://example.com/question/123456.html if not exist, I want to execute next rewrite rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
Other than return 404 to user.
I would write it this way:
location ~ /question-(.*)\.html$ {
try_files /question/$1.html /question.php?id=$1;
}

Nginx rewrite to include folder

I'm having a problem with my rewrite rule. It doesn't include folders in the rewrite path. For example:
/randomstring/app.js rewrites to /var/www/CDN/Dev/App/app.js
/randomstring/dashboard/app.js rewrites to /var/www/CDN/Dev/App/app.js but it should rewrite to /var/www/CDN/Dev/App/dashboard/app.js
I don't understand why it doesn't work. (.*) matches everything but a dot if I'm not mistaken so why doesn't it include the dashboard/ part?
location ~* (css|js)$ {
rewrite ^/([^/]*)/(.*).(css|js)$ /$2.$3 ;
root /var/www/CDN/Dev/App;
}
I see no reason to use rewrite here. Alias should be enough
location ~* /[^/]+(/.+\.(css|js))$ {
alias /var/www/CDN/Dev/App/$1;
}
location ~* \.(css|js)$ {
rewrite ^/([^/]+)/(.+)\.(css|js)$ /$2.$3 ;
root /var/www/CDN/Dev/App;
}

how do I rewrite a URL in nginx to an updated location?

I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}

Resources