Nginx核心配置-检测文件是否存在
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/",如下图所示
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12041444.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。