分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

解释try_files $uri $uri/ /index.php$is_args$args;

发布时间:2023-09-06 02:20责任编辑:顾先生关键词:暂无标签
nginx 里边 try_files的用法
核心作用:可以替代rewrite
作用域: server 、location
没有默认值

语法: try_files ??【$uri 】 ?【 $uri/ 】 ?参数
如:
try_files $uri $uri/ ?/index.php$is_args$args ????或 ?try_files ?$uri ?$uri/ = 404

$uri ??是请求文件的路径
$uri/ ??事请求目录的路径

参数: $uri
解释: 表示当前请求的URI,不带任何参数
访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I
返回: "/192.168.1.200"

原理:
(1)、当location匹配到以后,如 www.123.com/example ?????配置为 ?try_files ?$uri ?= 404
他只会在发布目录里边着examplt这个文件,(因为你只配置了$uri) ??

(2)、当你访问 www.123.com/example ?配置为 ?try_files ?$uri/ ?= 404 ??他会在发布目录里边寻找example 这个目录,(因为只配置了$uri/ )

(3)、当你的配置为 try_files ?$uri ?$uri/ = 404 ?,访问 www.123.com/example 的时候 ?会先在发布目录里边找example这个文件,如果没有的话 ?就在寻找example这个目录。在try_files 寻找完以后不会跳出location,而是往下执行,如果后边没有需要执行的,就会返回给客户端

(4)、当try_files 找不到文件时,内部会将这次请求重定向到最后一个参数,所以最后一个参数必须存在, try_files ?$uri ?$uri/ = 404 ?这个配置,当你访问 www.123.com/example 时找不到example文件或目录的时候,就会重定向到最后一个参数,就是 =404 ,当然也可以指向到其他的location,等等

(5)、/index.php$is_args$args

$is_args 解释: 表示请求中的URL是否带参数,如果带参数,$is_args值为"?"。如果不带参数,则是空字符串 访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I 返回: "?"
访问: curl http://test.wanglei.com/192.168.1.200 -I 返回: ""

参数: $args ???-----只接收参数
解释: HTTP请求中的完整参数。
访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I
返回: "a=10"

有参数的话 ??就变成了 /index.php?a=10 ??//这样就能访问了。

现在解释自己的配置:如下

server{
listen ??????80;
server_name example.123.cn;
index index.html index.htm index.php;
root ??/alidata/txall/callback/web;

location ?/ ?{
root ?/alidata/txall/123/web;
index ??index.html index.php index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
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;
}

定义到了 /alidata/txall/123/web。web里边有index.php,
当我访问 example.123.cn/index ?的时候,try_files ?会在web下找是否有index文件和目录(因为配置了 $uri 和 $uri/ ), 很显然web目录里边没有,所以就把index当成了参数,所以呢原来的 example.123.cn/index ??就变成了 ?example.123.cn/index.php?index ???就这样。

解释try_files $uri $uri/ /index.php$is_args$args;

原文地址:http://blog.51cto.com/13930997/2311716

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved