2016-10-12 89 views
1

我有一個反向的nginx代理我想要的路線,與進來的所有請求:重路由nginx的請求URL

http://dns.com/content/xyz <—to—> http://dns.com/content/1.0/xyz

我具有上游:

upstream backend_api.content.com { 
     server localhost:8080 max_fails=5 fail_timeout=30; 
     keepalive 100; 
    } 

和位置:

#Content Service 
location ~* ^/content/?(.*) { 
    set $proxy_pass "http://backend_api.content.com"; 
    rewrite ^/content/1.0(/.*)$ /content/1.0$1 break; 
    proxy_pass $proxy_pass 
    proxy_http_version 1.1; 
    proxy_set_header Connection ""; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header Host "api.stg.xxx.com"; 
    proxy_set_header X-3scale-proxy-secret-token $secret_token; 
    proxy_set_header Original-Host $http_host; 
    proxy_set_header Authorization $outbound_auth_header; 
    proxy_set_header Original-Uri $scheme://$http_host$uri; 
    post_action /out_of_band_oauth_authrep_action; 
    } 

但它看起來像任何與http://dns/content/xyz fa ils,只有當我給http://dns/content/1.0/xyz它工作。

回答

1

您好像在捕獲location ~* ^/content/?(.*)聲明中的URI的一部分,但是對此沒有做任何處理。

您還有一個rewrite ^/content/1.0(/.*)$ /content/1.0$1 break;語句,它什麼都不做,它只是寫回相同的URI。

一個快速和骯髒的解決方案可能是使用兩個rewrite語句是這樣的:

rewrite ^/content/1.0(/.*)$ /content/1.0$1 break; 
rewrite ^/content(/.*)$ /content/1.0$1 break; 

這意味着任何不第一個(非)改寫比賽將通過第二處理,並得到插入一個/1.0

就個人而言,我不喜歡它,而寧願使用兩個定位塊:

location /content/1.0 { 
    set $proxy_pass "http://backend_api.content.com"; 
    proxy_pass $proxy_pass; 
    proxy_http_version 1.1; 
    proxy_set_header ... 
    ... 
} 
location /content { 
    rewrite ^/content(/.*)$ /content/1.0$1 last; 
} 

但檢查你的其他location塊的計算順序。請注意,前綴位置塊和正則表達式位置塊具有不同的評估規則。詳情請見this document