2013-05-13 15 views

cevap

16
location = /oneapi { 
    set $args $args&apiKey=tiger; 
    proxy_pass https://api.somewhere.com; 
} 
+4

$ Eğer args sen $ args ayarlamak boş etmek ve ap iKey = kaplan. Bu doğru değil. – Jack

+0

Gerçekte, cevabınızı çok hızlı bir şekilde doğruladım ve düzenlenen çözüm işe yaradı. Sorunun $ args boş değil =>? Field = 22 & apiKey = kaplan – LeGilles

+2

Bu bir 'location/somepath 'bloğu (eşittir işareti yok) kullanırken işe yaramaz. Bunun yerine, aynı noktada yeniden yazmak için^(. *) $ $? $ Args & myParam = value break; – nerdherd

2

İşte https://gist.github.com/anjia0532/da4a17f848468de5a374c860b17607e7

#set $token "?"; # deprecated 

set $token ""; # declar token is ""(empty str) for original request without args,because $is_args concat any var will be `?` 

if ($is_args) { # if the request has args update token to "&" 
    set $token "&"; 
} 

location /test { 
    set $args "${args}${token}k1=v1&k2=v2"; # update original append custom params with $token 
    # if no args $is_args is empty str,else it's "?" 
    # http is scheme 
    # service is upstream server 
    #proxy_pass http://service/$uri$is_args$args; # deprecated remove `/` 
    proxy_pass http://service$uri$is_args$args; # proxy pass 
} 

#http://localhost/test?foo=bar ==> http://service/test?foo=bar&k1=v1&k2=v2 

#http://localhost/test/ ==> http://service/test?k1=v1&k2=v2 
+1

kod ve bunun nedenini açıkladıysanız cevap daha iyi olurdu. – ADyson

+0

@ADyson Üzgünüm, ingilizcem çok iyi değil, ama üzerinde çalışıyorum. Cevabımı geliştireceğim. – AnJia

+0

Kapat, ama burada '$ args' boş olduğunda,' http: // localhost/test/'aslında' http: // service/test? 'I yeniden yönlendirir, k1 = v1 & k2 = v2' (iki integorikasyon işareti) boş olsa bile, başka bir dizeyle birleştirildiğinde nginx '$ is_args'' '' 'döndürme neden olur. Son ifadeden '$ is_args'nın kaldırılması,' $ args'ın boş olmadığı zaman önceki simgeden yoksun olmasına neden olacaktır. – Mahn

0

github yüreğin (orijinal URL argümanları vardı ya da olmasın bilinmeyen olduğunda nginx bir parametrenin yeni eklemek için bir yoldur, yani her iki ? hesaba varken ve &):?

location /oneapi { 
    set $pretoken ""; 
    set $posttoken "?"; 

    if ($is_args) { 
     set $pretoken "?"; 
     set $posttoken "&"; 
    } 

    # Replace apiKey=tiger with your variable here 
    set $args "${pretoken}${args}${posttoken}apiKey=tiger"; 

    # Optional: replace proxy_pass with return 302 for redirects 
    proxy_pass https://api.somewhere.com$uri$args; 
} 
İlgili konular