2017-07-15 110 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

希望这是你想要的!