2017-07-15 45 views
1

下面我有nginx的配置如何發送REQUEST_URI作爲查詢參數proxy_pass在nginx的

server { 
listen 80; 
client_max_body_size 10M; 
keepalive_timeout 15; 
server_name mysite.com; 

location/{ 
    proxy_pass http://anothersite.com 
} 
} 

以上工作,但我需要的東西象下面這樣:

位置/ { proxy_pass http://anothersite.com?q=request_uri這樣我就可以將request_uri作爲查詢參數傳遞。

您能否提供正確的語法來傳遞request_uri作爲查詢參數。

謝謝。

回答

0

您可以使用正則表達式簡單rewriterequest_uri

location/{ 
    rewrite ^/(.+)$ ?q=$1 
    proxy_pass http://anothersite.com; 
} 

rewrite規則符合任何request_uri開始/並捕獲它自帶之後的任何非空字符串(+)。然後它重寫request_uri?q=,然後是/之後的任何內容。由於您的proxy_pass指令沒有以/結尾,因此它會將重寫的request_uri附加到代理目標。

所以要http://yoursite.com/some/api的請求將被重寫和代理傳遞到http://anothersite.com?q=some/api

希望這是你想要的!