5

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
  • 51

1 Answers1

5

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
  • 38,887