一个重定向的应用配置:
server {
listen 8000;
server_name localhost;
root F:/home/projects/test;
index index.php;
location / {
if (-f $request_filename) {
rewrite ^/(.+)$ /index.php last;
}
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
.........
}
测试url:http://127.0.0.1:8000/control_name/action_name?test=123&test2=abc
location优先级说明:http://www.cnblogs.com/tdkj/p/3440120.html
location匹配的是$document_uri,$document_uri 会随着重定向而改变;
测试的url会匹配到location /,然后找不到$request_filename(物理文件),就会重定向到http://127.0.0.1:8000/index.php,重定向后,$document_uri会变成/index.php,匹配到 location ~ .php$进行php-cgi处理,此过程中$request_uri也就是php中$_SERVER['REQUEST_URI']的值是不会变的,但处理的物理文件变成了F:/home/projects/test/index.php,index.php可以通过解析/control_name/action_name?test=123&test2=abc得到控制器,action……实现mvc。
index.php
<?php
echo $_SERVER['REQUEST_URI'];
echo "<br/>";
echo $_SERVER['SCRIPT_FILENAME'];
echo "<br/>";
echo $_SERVER['QUERY_STRING'];
?>
执行结果:
/control_name/action_name?test=123&test2=abc F:/home/projects/test/index.php test=123&test2=abc