4

Can you help me? I'm trying to block all requests outside my domain, and allow the request if a specified parameter exists

location ~* \.(mp4|vtt|mp3|mkv|avi)$ {
        if ( $http_referer !~* 'mywebsite.com' ) { # check http_referer
               if ($arg_78up = '') { #check if parameter 78up is empty
                        return 404; # then redirect to 404 page
               }
        }
}

And I'm getting this error when nginx starts:

nginx: [emerg] "if" directive is not allowed here in /etc/nginx/sites-enabled/default:17

What should I do to fix this issue? Thank you!

Vixarc
  • 41
  • 1
  • 1
  • 2

1 Answers1

4

nginx does not allow nested if statements. You need to use map functionality to achieve your goal.

In the http level, add the map:

map "$http_referer:$arg_78up" $refernotok {
    default 1;
    ~ mywebsite.com.*:.*$ 0;
    ~ ^.+:.+$ 0;
}

if ($refernotok = "1") {
    return 404;
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • nginx: [emerg] unknown "referok1$referok2" variable – Vixarc Feb 27 '19 at 09:23
  • Sorry for that, I had wrong assumption about how variables could be used. I updated the answer with a different version, which could work. – Tero Kilkanen Feb 27 '19 at 18:44
  • I'm traveling, when I come back, I'll test and mark your answer as correct. Please, can you explain what "^.+:.+$ 0" means? Thank you!! – Vixarc Feb 28 '19 at 11:16
  • First part is regular expression. `^` means start of string, `.+` means one or more characters, `:` is a delimiter character, and then again we match one or more characters with `.+`, and finally we require the string ending with `$`. 0 is the value `$refernotok` variable gets when the regular expression matches. – Tero Kilkanen Feb 28 '19 at 20:11
  • Hey, sorry the late, I'm back now! I'm getting this error: `nginx: [emerg] invalid number of the map parameters in /etc/nginx/nginx.conf:35`. How can I fix it? – Vixarc Mar 08 '19 at 08:18