How to group different rewrite rules in nginx? - 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.

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 rewrite not working - returning 404

I'm trying to rewrite :
http://example/test/ -> http://example/new/
http://example/test/check -> http://example/new/check
location ~/test/(.*)$ {
rewrite ^/new/$1?$args permanent;
}
What am I doing wrong?
Your rewrite statement is incorrect. See this document for more.
To use rewrite to capture the latter part of the URI, try:
rewrite ^/test/(.*)$ /new/$1 permanent;
Alternatively, to use location to capture the latter part of the URI, try:
location ~ ^/test/(.*)$ {
return 301 /new/$1?$args;
}

If Is Evil - Nginx

I'm using Nginx 1.6.2. I read that if () is evil and it's not good using it so I need a bit help, because I can't do what I want without using if(). I will post the rules I have with if and would ask if somebody could help me and tell me how to not use if () and use something else and get the same result.
# REDIRECT NON-WWW TO WWW.
if ($http_host != "www.site.eu") {
rewrite ^ http://www.site.eu$request_uri permanent;
}
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
# REMOVE ANY MULTIPLE SLASHES IN THE URL.
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) $scheme://$host/$1 permanent;
}
First rule should be replaced with separate server blocks
server {
listen 80 default_server;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
# normal config
}
Other ifs usually are not necessary. Just don't generate links with index.php and you will not need to strip it.
In the official wiki introduction it says that there are some cases which are ok. Have a look at this quote:
The only 100% safe things which may be done inside if in location
context are:
return ...; rewrite ... last;
At the end of the introduction there is an example which also features a rewrite command. So your code looks ok, too.
EDIT: You should also have a look at how the if works.
You can replace this block
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
with this
location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
I also don't think you need to worry about the last rule for double // because nginx by default automatically takes care of that before it even gets to the point of matching location blocks

Redundant Nginx Location and Rewrite Regular Expression

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

Resources