'nginx: [emerg] "if" directive is not allowed here in /etc/nginx/nginx.conf:64

So I am trying to block certain web browsers (not that you need to know) and everyone says to add if ($http_user_agent ~* (Chrome) ) { return 403; }

I do all of that and when I run nginx -t it says this: nginx: [emerg] "if" directive is not allowed here in /etc/nginx/nginx.conf:64

Do you guys know why?



Solution 1:[1]

Nginx only allowed if to be placed in a server or location block. if should also not be in nginx.conf mostly, while it should be on /etc/nginx/sites-enabled/name.conf or /etc/nginx/sites-available/name.conf.

You also can't put a server or location block in nginx.conf because it would just give another error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FishyMine