2012-05-16 35 views
13

tôi mới để nginx, đến từ apache và tôi về cơ bản muốn làm như sau:Nginx proxy hoặc viết lại tùy thuộc vào user agent

Dựa trên user-agent: iPhone: chuyển hướng đến iphone.mydomain.com

android: chuyển hướng đến android.mydomain.com

facebook: reverse proxy để otherdomain.com

bài khác: chuyển hướng đến ...

.210

và thử nó theo cách sau:

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

Nhưng bây giờ tôi nhận được một lỗi khi khởi động 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" 

Và tôi không nhận được như thế nào để làm điều đó hay vấn đề là gì.

Cảm ơn

Trả lời

18

Các '/ api' một phần của mục tiêu proxy_pass là phần URI được thông báo lỗi được đề cập đến. Vì ifs là các vị trí giả, và proxy_pass với phần uri thay thế vị trí phù hợp với uri đã cho, nó không được phép trong một if. Nếu bạn chỉ đảo ngược logic đó nếu bạn có thể làm việc này:

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; 
} 
Các vấn đề liên quan