title: Nginx proxy or rewrite depending on user agent
body: <p>i'm new to nginx, comming from apache and i basically want to do the following:</p>
  
  <p>Based on user-agent:
  iPhone: redirect to iphone.mydomain.com</p>
  
  <p>android: redirect to android.mydomain.com</p>
  
  <p>facebook: reverse proxy to otherdomain.com</p>
  
  <p>all other: redirect to ...</p>
  
  <p>and tried it the following way:</p>
  
  <pre><code>location /tvoice {
     if ($http_user_agent ~ iPhone ) {
      rewrite     ^(.*)   https://m.domain1.com$1 permanent;
     }
     ...
     if ($http_user_agent ~ facebookexternalhit) {
      proxy_pass         http://mydomain.com/api;
     }
  
     rewrite     /tvoice/(.*)   http://mydomain.com/#!tvoice/$1 permanent;
  }
  </code></pre>
  
  <p>But now i get an error when starting nginx:</p>
  
  <pre><code>nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except"
  </code></pre>
  
  <p>And i dont get how to do it or what the problem is.</p>
  
  <p>Thanks</p>