There are some situations for which Nginx config file does not provide adequate solutions. Very often, using variables, both built-in and user-defined, might help.
For example, Nginx does not support complex conditions, neither as logical operations nor as nested if's. The solution is to define a new variable. If you want a certain URL rewrite to take place only if the Referer header contains "somedomain.com" OR is empty, you can write:
$dorewrite = 'no';
if ($http_referer ~* somedomain.com) {
set $dorewrite 'yes';
}
if ($http_referer = '') {
set $dorewrite 'yes';
}
if ($dorewrite = 'yes') {
rewrite ^ new-request;
}
If you want to rewrite the URL if both conditions are true, for example, the Referer header contains "somedomain.com" AND there's a GET parameter 'id' equal to '12345', modify the snippet above using the De Morgan's law:
$dorewrite = 'yes';
if ($http_referer !~* somedomain.com) {
set $dorewrite 'no';
}
if ($arg_ID != '12345') {
set $dorewrite 'no';
}
if ($dorewrite = 'yes') {
rewrite ^ new-request;
}
Note the variable $arg_ID. This is a built-in variable. There is one such variable for every GET parameter in the request. Its name is composed of $arg_ and the name of the parameter. If you know all parameters you need to serve the request, these variables will help you to extract and pass them around.
If you use complicated regular expressions which you'd like to simplify, define another variable that would contain a part of the regexp:
if ($arg_ID ~* ^doc-number-([-a-z0-9]*)$) {
set $newarg $1;
}
if ($dorewrite = 'yes') {
rewrite ^ /doc/view/$newarg? break;
}