首页 > 系统相关 >Nginx 的 try_files 指令使用实例

Nginx 的 try_files 指令使用实例

时间:2023-06-20 17:44:59浏览次数:56  
标签:files index uri try Nginx location root

Nginx的配置语法灵活,可控制度非常高。在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率。

try_files指令说明

try_files指令
语法:try_files file ... uri 或 try_files file ... = code
默认值:无
作用域:server location

其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。

需要注意的是,只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。命名的location也可以使用在最后一个参数中。与rewrite指令不同,如果回退URI不是命名的location那么$args不会自动保留,如果你想保留$args,则必须明确声明。


try_files $uri $uri/ /index.php?q=$uri&$args;

实例分析

示例一

try_files 将尝试你列出的文件并设置内部文件指向。

例如:

try_files /app/cache/ $uri @fallback; 
index index.php index.html;

它将检测$document_root/app/cache/index.php,$document_root/app/cache/index.html 和 $document_root$uri是否存在,如果不存在着内部重定向到@fallback(@表示配置文件中预定义标记点) 。
你也可以使用一个文件或者状态码(=404)作为最后一个参数,如果是最后一个参数是文件,那么这个文件必须存在。

示例二

例如nginx不解析PHP文件,以文本代码返回

try_files $uri /cache.php @fallback;

因为这个指令设置内部文件指向到 $document_root/cache.php 并返回,但没有发生内部重定向,因而没有进行location段处理而返回文本 。
(如果加上index指令可以解析PHP是因为index会触发一个内部重定向)

示例三

跳转到变量

server {
 listen 8000;
 server_name 192.168.119.100;
 root html;
 index index.html index.php;
 
 location /abc {
     try_files /4.html /5.html @qwe;      		#检测文件4.html和5.html,如果存在正常显示,不存在就去查找@qwe值
}

 location @qwe  {
    rewrite ^/(.*)$   http://www.baidu.com;       #跳转到百度页面
 }

示例四

跳转指定文件

server {
   listen 8000;
   server_name 192.168.119.100;
   root html;
   index index.php index.html;

   location /abc {
       try_files /4.html /5.html /6.html;
  }

示例五

将请求跳转到后端

upstream tornado {
        server 127.0.0.1:8001;
}
 
server {
        server_name imike.me;
        return 301 $scheme://www.imike.me$request_uri;
}
 
server {
        listen 80;
        server_name www.imike.me;
 
        root /var/www/www.imike.me/V0.3/www;
        index index.html index.htm;
 
        try_files $uri @tornado;
 
        location @tornado {
                proxy_pass_header Server;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
 
                proxy_pass http://tornado;
        }
}

常见错误

常见错误一

try_files 按顺序检查文件是否存在,返回第一个找到的文件,至少需要两个参数,但最后一个是内部重定向也就是说和rewrite效果一致,前面的值是相对$document_root的文件路径。也就是说参数的意义不同,甚至可以用一个状态码 (404)作为最后一个参数。如果不注意会有死循环造成500错误。

location ~.*\.(gif|jpg|jpeg|png)$ {
        root /web/wwwroot;
        try_files /static/$uri $uri;
}

原意图是访问http://example.com/test.jpg时先去检查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg

但由于最后一个参数是一个内部重定向,所以并不会检查/web/wwwroot/test.jpg是否存在,只要第一个路径不存在就会重新向然后再进入这个location造成死循环。结果出现500 Internal Server Error

location ~.*\.(gif|jpg|jpeg|png)$ {
        root /web/wwwroot;
        try_files /static/$uri $uri 404;
}

这样才会先检查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg再不存在则返回404 not found

常见错误二

Nginx try_files $query_string为空的解决办法

server {
    listen 80;
    server_name localhost.dev;
    index index.php index.html index.htm;
    set $root_path '/var/www/phalcon/public'; 
    root $root_path;
    location / {
        try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
    }
    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }
    location ~ /\.ht {
        deny all;
    }
}

发现PHP无法获取$_GET信息

try_files $uri $uri/ /index.php;

改为


try_files $uri $uri/ /index.php?$query_string;

即可解决

标签:files,index,uri,try,Nginx,location,root
From: https://www.cnblogs.com/da-datang/p/17494290.html

相关文章

  • nginx:报错upstream sent too big header(nginx 1.24)
    一,报错信息:访问网站时报错:如图: 查看nginx的错误日志:2023/06/1610:21:46[error]416087#0:*71148upstreamsenttoobigheaderwhilereadingresponseheaderfromupstream,client:223.72.69.14,server:blog.liuhongdi.com,request:"GET/index......
  • 【问题解决】 网关代理Nginx 301暴露自身端口号
    一般项目上常用Nginx做负载均衡和静态资源服务器,本案例中项目上使用Nginx作为静态资源服务器出现了很奇怪的现象,我们一起来看看。“诡异”的现象部署架构如下图,Nginx作为静态资源服务器监听8080端口,客户浏览器通过API网关的443端口(就是https)获取Nginx静态资源。现象是用户浏览......
  • nginx+keepalived
    nginx:1.正向代理:访问转到代理服务,然后去访问正式的地址。2.反向代理:通过一个入口,进行请求转发。3.负载均衡:不同的解析服务器(比如tomcat)进行负载均衡。4.动静分离:将静态的与需要服务器解析分开,以提高访问速度。keepalived:健康检测。为负载均衡而生。如果服务器出现故障......
  • 将 Vue 项目部署到 Nginx 上
    将Vue项目部署到Nginx上安装Nginx下载地址:nginx:download(1)因为我在Windows系统下安装Nginx,所以选择nginx/Windows-1.22.1。下载的资源是一个压缩包,解压后即可使用。(2)打开命令行提示符(cmd),切换到Nginx的根目录,启动Nginx服务器。cd/DD:/software/nginx-1.22.1......
  • BindingException异常:Type interface com.niuyun.dao.UserDao is not known to the Ma
    Mybatis出现:org.apache.ibatis.binding.BindingException:Typeinterfacecom.niuyun.dao.UserDaoisnotknowntotheMapperRegistry.的错误,如何解决?错误如下:类型接口dao不知道mapper注册中心点的问题org.apache.ibatis.binding.BindingException:Typeinterfacecom.niuy......
  • BUUCTF:[SUCTF 2019]Pythonginx
    @app.route('/getUrl',methods=['GET','POST'])defgetUrl():url=request.args.get("url")host=parse.urlparse(url).hostnameifhost=='suctf.cc':return"我扌yourproblem?111&q......
  • Liunx nginx服务
    目录一、nginx概念二、nginx特点三、nginx应用场景四、nginx和apache五、阻塞和非阻塞六、同步和异步七、编译安装nginx八、升级nginx九、总结     一、nginx概念1.nginx概念Nginx("enginex")是一个高性能的HTTP和反向代理服务器。Nginx......
  • nginx 1.25. 1 发布
    nginx1.25.1有一个很不错的特性,就是支持了http2指令,以前这个指令主要是也listen配置使用的(ssl+http2场景)独立指令之后就有了很方便的功能了,比如有些业务希望使用http0.9-1.1协议,有些需要使用http2,当然目前也是支持了http3的,可以做到分离,以前版本存在一个问题就是开启了......
  • nginx配置多个配置文件,nginx配置多个conf的方式 播报文章
    可以通过在nginx.conf文件中使用include关键字来引入多个子配置文件,从而实现对Nginx的多配置管理。下面是简单的操作步骤:  1.进入Nginx的conf目录(通常是/etc/nginx或者/usr/local/nginx/conf),创建一个名为conf.d的目录,用于存放多个子配置文件:  mkdir......
  • nginx前端页面通过docker部署过程中的相关问题
    1、nginx.conf的ip地址对应服务器的ip 2、数据卷的路径需要与配置文件对应nginx.conf创建数据卷与容器的语句dockerrun--name=nginx01-vhtml:/usr/share/nginx/html-p8080:80-dnginx3、nginx.conf配置文件的存放位置dockercpnginx.confngin......