2012-05-16 27 views
13

soy nuevo Nginx, que vienen de Apache y Básicamente quiero hacer lo siguiente:Nginx proxy o rewrite en función de agente de usuario

Sobre la base de agente de usuario: iPhone: redirigir a iphone.mydomain.com

androide: redirigir a android.mydomain.com

facebook: proxy para revertir otherdomain.com

todos los demás: redirigir a ...

y probamos la siguiente manera:

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; 
} 

Pero ahora me sale un error al iniciar nginx:

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" 

Y no me llevo cómo hacerlo, o cuál es el problema.

Gracias

Respuesta

18

La parte '/ api' del objetivo PROXY_PASS es la parte URI el mensaje de error se refiere. Dado que ifs son pseudo-ubicaciones, y proxy_pass con una parte uri reemplaza la ubicación coincidente con el uri dado, no está permitido en un if. Si solo invierte esa lógica de if, puede hacer que esto funcione:

location /tvoice { 
    if ($http_user_agent ~ iPhone) { 
    # return 301 is preferable to a rewrite when you're not actually rewriting anything 
    return 301 https://m.domain1.com$request_uri; 

    # if you're on an older version of nginx that doesn't support the above syntax, 
    # this rewrite is preferred over your original one: 
    # rewrite^https://m.domain.com$request_uri? permanent; 
    } 

    ... 

    if ($http_user_agent !~ facebookexternalhit) { 
    rewrite ^/tvoice/(.*) http://mydomain.com/#!tvoice/$1 permanent; 
    } 

    proxy_pass   http://mydomain.com/api; 
} 
Cuestiones relacionadas