Nginx核心配置-检测文件是否存在

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。

一.try_files使用案例1

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
keepalive_timeout 65 65; #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/auth.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /login {
root /yinzhengjie/data/web/nginx;
index index.html;
try_files $uri /default.html;          #如果访问当前localtion出现了错误页面,都会被跳转到"/default.html"
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

3>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,255)'>Defalut</h1>" > /yinzhengjie/data/web/nginx/static/default.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/ -R
/yinzhengjie/data/web/nginx/:
total 0
drwxr-xr-x 2 root root 24 Dec 17 12:49 login
drwxr-xr-x 2 root root 44 Dec 17 12:54 static /yinzhengjie/data/web/nginx/login:
total 4
-rw-r--r-- 1 root root 171 Dec 17 09:41 index.html /yinzhengjie/data/web/nginx/static:
total 8
-rw-r--r-- 1 root root 46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root 73 Dec 17 12:54 index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/static/default.html
<h1 style='color:rgb(255,0,255)'>Defalut</h1>
[root@node101.yinzhengjie.org.cn ~]#

4>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4011 2840 0 12:38 ? 00:00:00 nginx: worker process
nginx 4012 2840 0 12:38 ? 00:00:00 nginx: worker process
nginx 4013 2840 0 12:38 ? 00:00:00 nginx: worker process
nginx 4014 2840 0 12:38 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4066 2840 2 12:42 ? 00:00:00 nginx: worker process
nginx 4067 2840 3 12:42 ? 00:00:00 nginx: worker process
nginx 4068 2840 3 12:42 ? 00:00:00 nginx: worker process
nginx 4069 2840 3 12:42 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

5>.浏览器访问一个不存在的页面,默认匹配到的是"http://node101.yinzhengjie.org.cn/default.html"。

二.try_files使用案例2

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/auth.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /login {
root /yinzhengjie/data/web/nginx;
index index.html;
#try_files $uri /default.html;
try_files $uri $uri.html $uri/index.html /default.html;        #根据给定的参数依次匹配,首先获取到用户输入的uri,然后用用户的uri的值添加".html"后缀进行配置,若不成功就继续访问uri目录下的index.html,若前面两个都匹配失败,则最后匹配"/defalut.html"。
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.编写测试数据

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/login/
total 4
-rw-r--r-- 1 root root 171 Dec 17 09:41 index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,0,255)'>Golang</h1>" > /yinzhengjie/data/web/nginx/login/go.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/data/web/nginx/login/test
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,255)'>TEST Page</h1>" > /yinzhengjie/data/web/nginx/login/test/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/ -R
/yinzhengjie/data/web/nginx/:
total 4
-rw-r--r-- 1 root root 43 Dec 17 13:04 index.html
drwxr-xr-x 3 root root 51 Dec 17 13:08 login
drwxr-xr-x 2 root root 44 Dec 17 12:54 static /yinzhengjie/data/web/nginx/login:
total 8
-rw-r--r-- 1 root root 43 Dec 17 13:08 go.html
-rw-r--r-- 1 root root 171 Dec 17 09:41 index.html
drwxr-xr-x 2 root root 24 Dec 17 13:09 test /yinzhengjie/data/web/nginx/login/test:
total 4
-rw-r--r-- 1 root root 48 Dec 17 13:09 index.html /yinzhengjie/data/web/nginx/static:
total 8
-rw-r--r-- 1 root root 46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root 73 Dec 17 12:54 index.html
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx |grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4434 2840 0 13:06 ? 00:00:00 nginx: worker process
nginx 4435 2840 0 13:06 ? 00:00:00 nginx: worker process
nginx 4436 2840 0 13:06 ? 00:00:00 nginx: worker process
nginx 4437 2840 0 13:06 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx |grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4497 2840 2 13:14 ? 00:00:00 nginx: worker process
nginx 4498 2840 2 13:14 ? 00:00:00 nginx: worker process
nginx 4499 2840 2 13:14 ? 00:00:00 nginx: worker process
nginx 4500 2840 3 13:14 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

4>.客户端验证

  浏览器输入:"http://node101.yinzhengjie.org.cn/login/test",匹配到了"http://node101.yinzhengjie.org.cn/login/test/index.html"的内容,如下图所示。

  浏览器输入:"http://node101.yinzhengjie.org.cn/login/go",匹配到的是:"http://node101.yinzhengjie.org.cn/login/go.html",如下图所示

  浏览器输入一个不存在的路径,默认匹配到的是"http://node101.yinzhengjie.org.cn/default.html",如下图所示。

三.try_files使用案例3

1>.编写子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/auth.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /login {
root /yinzhengjie/data/web/nginx;
index index.html;
#try_files $uri /default.html;
#try_files $uri $uri.html $uri/index.html /default.html;
try_files $uri $uri.html $uri/index.html /default.html =888;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4497 2840 0 13:14 ? 00:00:00 nginx: worker process
nginx 4498 2840 0 13:14 ? 00:00:00 nginx: worker process
nginx 4499 2840 0 13:14 ? 00:00:00 nginx: worker process
nginx 4500 2840 0 13:14 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 4604 2840 1 13:23 ? 00:00:00 nginx: worker process
nginx 4605 2840 2 13:23 ? 00:00:00 nginx: worker process
nginx 4606 2840 2 13:23 ? 00:00:00 nginx: worker process
nginx 4607 2840 2 13:23 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.浏览器访问"http://node101.yinzhengjie.org.cn/login/",如下图所示

4>.浏览器访问"http://node101.yinzhengjie.org.cn/login/test",如下图所示

5>.浏览器访问一个不存在的资源, "http://node101.yinzhengjie.org.cn/login/",如下图所示

Nginx 核心配置-检测文件是否存在的更多相关文章

  1. Nginx 核心配置详解

    目录 Nginx 核心配置详解 Nginx 四层访问控制: Nginx账户认证功能: 自定义错误页面: 自定义访问日志: 检测文件是否存在: 长连接配置: 作为下载服务器配置: 作为上传服务器: 其他 ...

  2. Nginx 核心配置-可优化配置参数

    Nginx 核心配置-可优化配置参数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx的官网查看指令帮助信息方法 1>.打开nginx的官网(https://ng ...

  3. Nginx 核心配置-作为上传服务器配置

    Nginx 核心配置-作为上传服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.关键参数说明 client_max_body_size 1m: 设置允许客户端上传单 ...

  4. Nginx 核心配置-作为下载服务器配置

    Nginx 核心配置-作为下载服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.无限速版本的下载服务器 1>.查看主配置文件 [root@node101.yinz ...

  5. Nginx 核心配置-自定义日志路径及清空日志注意事项

    Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...

  6. Nginx 核心配置-自定义错误页面

    Nginx 核心配置-自定义错误页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个h ...

  7. Nginx 核心配置-location的登录账户认证实战篇

    Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...

  8. Nginx 核心配置-location的匹配案例实战篇

    Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...

  9. Nginx 核心配置-新建一个web站点

    Nginx 核心配置-新建一个web站点 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx基础配置常用参数说明 [root@node101.yinzhengjie.or ...

随机推荐

  1. 基于CAS实现无锁结构

    杨乾成 2017310500302 一.题目要求 基于CAS(Compare and Swap)实现一个无锁结构,可考虑queue,stack,hashmap,freelist等. 能够支持多个线程同 ...

  2. 1-开发共享版APP(搭建指南)-快速搭建到自己的服务器

    该APP安装包下载链接: http://www.mnif.cn/appapk/IotDevelopmentVersion/20190820/app-debug.apk 或者扫描二维码下载 注:该下载可 ...

  3. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(APP用户程序制作)

    前言 用户程序比较简单,但是起着至关重要的作用 用户程序是和BootLoader程序相互配合的 拷贝文件到自己的项目 APP用户程序的 stmflash.c stmflash.h 和 上一节的Boot ...

  4. 关于const关键字

    const:ES6新增关键字,用于声明创建一个值的只读引用. 我们都知道,const一般用来定义常量,在声明的时候需要赋初始值,而且初始值一旦赋值,便不能改变. 但是以上说的是针对于基本类型数据的定义 ...

  5. CF717A Festival Organization(第一类斯特林数,斐波那契数列)

    题目大意:求 $\sum\limits_{n=l}^{r}\dbinom{f_n}{k}\bmod 10^9+7$.其中 $f_n$ 是长度为 $n$ 的 $01$ 序列中,没有连续两个或超过两个 $ ...

  6. [LeetCode] 789. Escape The Ghosts 逃离鬼魂

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...

  7. [HDU4867]Xor (线段树分治+类数位dp)

    [HDU4867]Xor (线段树分治+类数位dp) 提供一种\((m+n) log a log m\)带有常数约\(\frac{1}{log n}\)的算法 处理询问,将后来加入的数算进序列中,则每 ...

  8. 错误:PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。"+文件路径"的解决方案

    最近在使用python进行筛选图片的时候,想到用python里面的os库进行图片的删除. 具体筛选方法就是,删除掉图片长度或宽度小于100像素的图片,示例代码如下所示: for file in os. ...

  9. xss之挑战小靶场(1-10)

    在线靶场(http://xss.fbisb.com) w 第一关 get请求,没有什么过滤,直接上<script>alert()</script> 源码: 第二关 输入参数会显 ...

  10. UVA 583 分解质因数

    Webster defines prime as:prime (prim) n. [ME, fr. MF, fem. of prin first, L primus; akin to L prior] ...