Nginx通过子目录名重写多个GETvariables?

我目前正在使用nginx重写来传递服务器variables。 我可以像这样传递一个variables: http://example.com/test : http://example.com/test ,它可以通过: http://example.com/?p=test : http://example.com/?p=test 。 我这样做:

 location / { rewrite ^/([^/\.]+)$ /?p=$1 break; } 

现在我想能够使用子目录传递variables,以便映射发生像这样:

http://example.com/profile/user1 => http://example.com/?p=user1

我想要有这样的多个规则,以便在同一个网站上,我也有一个像这样的规则:

http://example.com/play/vid1 => http://example.com/?v=vid1

我试过这个,但没有奏效:

 location /profile/ { rewrite ^/([^/\.]+)$ /?p=$1 break; } location /play/ { rewrite ^/([^/\.]+)$ /?v=$1 break; } 

我也尝试没有像后面的斜杠:

 location /profile { rewrite ^/([^/\.]+)$ /?p=$1 break; } location /play { rewrite ^/([^/\.]+)$ /?v=$1 break; } 

我怎样才能做到这一点?

我能够像这样完成它:

 if ( $uri ~ "^/play$" ) { rewrite (.*) /; } if ( $uri ~ "^/play/([0-9A-Za-z]*)$" ) { rewrite ^/play/([0-9A-Za-z]*)$ /?ref=$1; } 

if在这里,你不应该使用。 在nginx中实现这些东西的正确方法是location块。 例如:

 location ~ /profile/(.+)$ { rewrite ^ /p=$1 break; } location ~ /play/(.+)$ { rewrite ^ /v=$1 break; } 

在这里,我们将path与location指令进行匹配,将目录后的部分捕获到variables中,并将其用于redirect。