这段内容摘自百度的某位作者,写的不错就拿过来了
try_files 理解:
以 try_files $uri $uri/ /index.php; 为例,当用户请求 http://servers.blog.ustc.edu.cn/example 时,这里的 $uri 就是 /example。try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是 WordPress 的安装目录)的文件,就直接把这个文件的内容发送给用户。显然,目录中没有叫 example 的文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。又找不到,就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到 http://servers.blog.ustc.edu.cn/index.php。这个请求会被 location ~ .php$ { … } catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI 中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响。
下面做一个伪静态
访问url:http://test.champzee.com/Index/about_us.html
实际url:http://test.champzee.com/index.php/Index/about_us.html
那么,我们希望当用户访问url的时候,他会通过nginx处理,重新定位至http://test.champzee.com/index.php/Index/about_us.html,但是对于用户来说是透明的,看到的还是访问时的地址
这个时候,就需要用到try_files参数,具体配置如下:
location / {
root /usr/local/www/test-cy-site;
try_files $uri /index.php$uri?$args;
}
生效前:
生效后:
还有一种伪静态,就是在请求的路径中将路径做一些追加,可采用这种形式
如:
访问地址 $uri 为 /weixin/xxx/xxx
真实地址 $uri 为 /weixin/index.php/xxx/xxx
location ~ .*\.(png|ico|jpg|jpeg|bmp|gif|js|css)$ {
root /usr/local/www/test-cy-site;
}
location /weixin {
proxy_pass http://test.champzee.com/weixin/index.php;
}