zoukankan      html  css  js  c++  java
  • nginx 的 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;

    二、示例

    所访问fn目录下如果文件不存在则跳转到指定域名

     [root@linux-node2 /etc/nginx/conf.d]# cat fn.conf 
    server {
        listen       80;
        server_name  192.168.5.72;
    
            location /fn {
                alias /usr/share/nginx/html/fn/;
                index index.html index.php;
                try_files $uri $uri/ @mudidi;
            }
            location @mudidi{
              proxy_pass http://www.baidu.com;
            }
    
    }
    
     [root@linux-node2 ~]# ls /usr/share/nginx/html/fn/
    a.txt  index  index.html  long.html

     跳转到指定文件,404.html文件需要存在:

            location /fn {
                alias /usr/share/nginx/html/fn;
                index index.html index.php;
                try_files $uri $uri/ 404.html;
            }

     如果文件或目录不存在,则跳转到指定的502错误:

            location /fn {
                alias /usr/share/nginx/html/fn;
                index index.html index.php;
                try_files $uri $uri/ =502;
            }
  • 相关阅读:
    图像的加载与保存
    numpy初学
    深入精通JavaScript插件
    Python图像处理库:Pillow 初级教程
    PIL包的应用
    UIWebView的离线缓存
    UITableView优化技巧
    UIKit Dynamics入门
    CALayer 一些重要属性
    一个Demo展示Storyboard的强大
  • 原文地址:https://www.cnblogs.com/cyleon/p/12403171.html
Copyright © 2011-2022 走看看