2012-05-16 3 views
13

이 nginx에하는 사용자 에이전트에 따라 아파치에서오고와 나는 기본적으로 다음을 수행 할 재 작성 :Nginx에 프록시 또는, 내가 새로 온

사용자 에이전트를 기반으로

: 아이폰 : iphone.mydomain.com로 리디렉션

안드로이드는 :

페이스 북을 android.mydomain.com로 리디렉션 : otherdomain.com하는 리버스 프록시

다른 모든 : ...에 재

,

하고 그것을 다음과 같은 방법으로 시도 :

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

을하지만 지금의 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" 

그리고 내가 그것을 또는 어떤 문제가 작업을 수행하는 방법을 얻을니까.

감사

답변

18

proxy_pass 대상의 '/ API'부분은 오류 메시지를 참조하는 URI의 일부분이다. ifs는 가상 위치이며 uri 부분을 가진 proxy_pass는 일치 된 위치를 주어진 uri로 대체하기 때문에 if에서는 허용되지 않습니다. 만약 당신이 그저 논리를 거꾸로 뒤집었다면, 이것을 작동시킬 수 있습니다 :

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; 
} 
관련 문제