Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx unknown directive "if($http_user_agent"

Tags:

nginx

I try to return 503 status code when the user agent header has a specific value. I tried outside and inside the location block. But when I reload the this config nginx failes to reload:

upstream api{
    server 127.0.0.1:1336;
}

# the nginx server instance
server {
    listen 0.0.0.0:80;
    server_name api.project.com;

   # if($http_user_agent = "android") {
   #     return 503;
   # }

    # pass the request to the node.js server with the correct headers
    location / {

     # if($http_user_agent = "android") {
     #   return 503;
     # }

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://api/;
      proxy_redirect off;
    }
}

Any ideas whats wrong? I am using nginx/1.4.7

From syslog:

nginx: [emerg] unknown directive "if($http_user_agent"
like image 621
UpCat Avatar asked Dec 01 '22 15:12

UpCat


1 Answers

Add a space between if and (. that should do the trick!

like image 94
VF_ Avatar answered Dec 10 '22 03:12

VF_