Redundant Nginx Location and Rewrite Regular Expression - nginx

I have a location and rewrite directive pair that works, but there is a redundancy that it seems like I should be able to optimize out. It takes an external URL like /css/20141201-styles.css and delivers /css/styles.css.
location ~ '^/(css|js)/[0-9]{8}-' {
rewrite '^/(css|js)/[0-9]{8}-(.*)$' /$1/$2;
}
It seems like I'm doing work twice, once in matching, and then again when rewriting. Is there a way to capture the matches in the location directive, and then use them in the rewrite?
location ~ '^/(css|js)/[0-9]{8}-(.*)$' {
rewrite [something here?] /$1/$2;
}
In Apache, it looks like this:
RewriteRule ^/(css|js)/[0-9]{8}-(.*)$ /$1/$2 [NC,L]
SOLUTION
Thanks to Terra for pointing out the alias directive! This location was already nested in another location which applies the root directive, so that takes care of the server path.
location ~ ^/(css|images|js)/ {
location ~ '^/(css|js)/[0-9]{8}-(.*)$' {
alias /$1/$2;
}
root /server/path/to/web/root;
}
UPDATE
This also works, and has fewer lines. The break flag on the rewrite directive prevents looping of the server level, making it just as performant as the alias solution.
location ~ ^/(css|images|js)/ {
rewrite '^/(css|js)/[0-9]{8}-(.*)$' /$1/$2 break;
root /server/path/to/web/root;
}

Solution 1: Do not create separate location, just move rewrite to other one. location / for example.
Solution 2:
location ~ ^/(css|js)/[0-9]{8}/ {
alias /server/path/to/web/root/$1/;
}

Related

how to create an nginx rewrite to add / to url

I'm trying to create an nginx rewrite that will rewrite /pagename to /pagename/
I've tried using:
location ~ "^/test$" {
rewrite /test /test/ break;
}
but that doesn't seem to work...
any help would be appreciated.
The first parameter of a rewrite statement is a regular expression, and the flag you should use is probably permanent (see this document for details), for example:
location ~ "^/test$" {
rewrite ^(/test)$ $1/ permanent;
}
But you do not need to match the regular expression twice, once in the location and again in the rewrite, so a return would be more efficient, for example:
location ~ "^(/test)$" {
return 301 $1/$is_args$args;
}
Also, the location matches a single URI, to the = operator would be better than a regular expression (see this document for details). So the preferred solution is:
location = /test {
return 301 $uri/$is_args$args;
}
I ended up using return:
return 301 /test/;

Location redirect all /*.xml to /*

I want to redirect all /filename.xml files to /filename.
I can redirect an individual file like this
location = /mascus-export.xml {
rewrite .* /mascus-export redirect;
}
but rather than listing each .xml file like individually, I want redirect all. Something like this:
location = /*.xml {
rewrite .* /* redirect;
}
I've tried this type of thing in every combination
location = /(*)$.xml {
rewrite .* /$1 redirect;
}
but nothing seems to work.
You are trying to write a regular expression location block, which begins with ~ and not =. See this document for details.
For example:
location ~ \.xml$ { ... }
The rewrite needs to capture part of the URI using parentheses. For example:
rewrite ^(.*)\.xml$ $1 redirect;
The rewrite will work perfectly well either enclosed within the above location block, or just naked within the enclosing server block.
If you are going to use a regular expression location block, you do not need to use a rewrite statement too. Use a return statement instead. For example:
location ~ ^(.*)\.xml$ {
return 302 $1;
}

nginx server: how to remove first directory from URL

Can anybody please help me to remove first directory name from URL?
My Image location is _data/hotel/3/15377/hotel_image.jpg
But Image path gets changed due to relative URL in code and it become to something like this.
example.com/france/_data/hotel/3/15377/hotel_image.jpg
example.com/usa/_data/hotel/3/15377/hotel_image.jpg
example.com/india/_data/hotel/3/15377/hotel_image.jpg
is their any possibilities to remove dynamic country name from above URL
If you want to rewrite only this particular URL, you can use this location block in your config:
location ~ /[a-z]+/_data/hotel/3/15377/hotel_image.jpg {
try_files /_data/hotel/3/15377/hotel_image.jpg;
}
If you want to rewrite all URLs which lead to /<country>/_data/..., you can use:
location ~ /[a-z]+/_data/(.+) {
try_files /_data/$1;
}
or for stricter URL checking:
location ~ /(?:france|usa|india)/_data/(.+) {
try_files /_data/$1;
}
#Ivan Shatsky's answer is great for files but also if we want to redirect a general url is better if you use the rewrite directive.
Depending where you define the rewrite directive you have two ways to implement it:
A. In the server context
server {
...
rewrite ^/[a-z]+/_data/(.+)$ /_data/$1 last;
...
}
B. In the location context
location ~ /[a-z]+/_data/(.+) {
rewrite ^/[a-z]+/_data/(.+)$ /_data/$1 break;
proxy_pass http://backend;
}
Teo, why did you change the flag to break?* Because, if this directive is put inside of a location context, the last flag might make nginx to run 10 cycles and return the 500 error.
Note:
Remember not add / at the end of the proxy_pass directive. This example wont work:
...
proxy_pass http://backend/;
...

How to group different rewrite rules in nginx?

I would like to have a lighter code for one of my NGINX configurations. I need to write the following four lines in only one (if possible!):
location /en/contact {
rewrite ^/.* https://$server_name/contact permanent;
}
location /en/quality {
rewrite ^/.* https://$server_name/quality permanent;
location /de/contact {
rewrite ^/.* https://$server_name/contact permanent;
}
location /de/quality {
rewrite ^/.* https://$server_name/quality permanent;
}
Thanks!
Using minimal typing, you could use a rewrite statement outside of any location block:
rewrite ^/(en|de)/(contact|quality) https://$server_name/$2 permanent;
If the scheme and server name does not change, the second parameter can be replaced with just /$2. See this document for more.
Alternatively, you could use a regular expression location block:
location ~ ^/(en|de)/(contact|quality) {
return 301 https://$server_name/$2;
}
Same as above, regarding the scheme and server name. Note that the evaluation order for regular expression location blocks is significant - the first rule that matches wins. See this document for more.

Basic nginx URL rewriting issue

I'm completely new to nginx and I was wondering if someone could give me a hand.
What I'm trying to to is turn this:
domain.com/ngu/short.php?t=123
into
domain.com/t/123
If someone could explain to me how it's done, I would be very grateful. I have looked around the site for a similar situation, but I haven't found anything exactly the same so I'm having some difficulty. Thank you for your time :).
the following location block does the rewrite:
location ~ /ngu/short.php {
if ($args_t) {
rewrite ^ http://$host/t/$v? last;
}
}
but that requires you enumerate all possible arguments, if you have lots of potential arguments it's probably easier to do
location ~ /ngu/short.php {
if ($args ~ "([a-z]+)=(\d+)") {
set $p $1;
set $v $2;
rewrite ^ http://$host/$p/$v? last;
}
}
explanation:
we do a location that matches on the /ngu/short.php path
the first code block above checks if we have a t=value argument and if so does the rewrite
the second code block above checks for a more general argument=something. We then need to save the backreferences in variables because a rewrite rule resets the backreferences which prevents using them directly
in either case if the if fails to match a 404 will be returned
$host and $args en $args_ are standard nginx variables see http://wiki.nginx.org/HttpCoreModule#Variables
we add a ? ad the end of the rewrite to keep nginx from readding the url arguments
In case this helps anyone else, this is what I (actually a friend) ended up doing:
location / {
root /home/jim/www;
index index.html index.htm index.php;
if (!-f $request_filename) {
rewrite ^/t/(.+)$ /ngu/short.php?t=$1 last;
rewrite ^/u/(.+)$ /ngu/short.php?u=$1 last;
rewrite ^/s/ /ngu/short.php last;
break;
}
}
I don't understand it, but it works.

Resources