JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

nginx 的try_files $uri $uri/ xxxxx用法

wys521 2024-10-12 23:13:01 精选教程 27 ℃ 0 评论

nginx 的try_files $uri $uri/ xxxxx用法

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

1.try_files $uri $uri/ xxxxx理解

当访问的地址中不匹配$uri(用户访问地址)和$uri/(用户访问地址/下面)时候,会跳转到定义的最后的xxxx页面

2.案例

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf |grep vhost

include vhost/*.conf;

[root@localhost ~]# cat /usr/local/nginx/conf/vhost/www.1.conf

server{

listen 80;

server_name www.1.com;

location /abc/ {

root /data/wwwroot/www.1.com/;

index index.html;

try_files $uri $uri/ /abc/a/index.html;

#当http://www.1.com/abc 和http://www.1.com/abc/下的路径匹配不上时候,如:http://www.1.com/abc/xxxxx,跳转到最后一个:/abc/a/index.html

}

}

[root@localhost ~]# ls /data/wwwroot/www.1.com/

abc

[root@localhost ~]# tree /data/wwwroot/www.1.com/abc/

/data/wwwroot/www.1.com/abc/

├── 1.html

├── 2.html

├── a

│ ├── a.html

│ └── index.html

├── b

│ ├── b.html

│ └── index.html

└── index.html

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/1.html

111

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/2.html

222

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/index.html

abc_index

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/a/a.html

aaa

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/a/index.html

a_index

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/b/b.html

bbb

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/b/index.html

b_index

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

当访问地址中匹配上前面两个定义的地址时候,则进行正常的访问,如下: $uri $uri/

当访问地址中没有匹配上前面两个定义的地址时候,则进会跳转到最后定义的地址,如下: /abc/a/index.html

用章生产环境配置案例:

location / {

root registry/static_root;

index index.html index.htm;

}

location /ElectronicSealManagement/ {

root registry/static_root;

index index.html index.htm;

try_files $uri $uri/ /ElectronicSealManagement/index.html;

}

# ls /home/essapp/nginx/registry/static_root/ElectronicSealManagement/

index.html serverConfig.json static

访问: http://59.198.247.62:10004/ElectronicSealManagement/seal

如果不配置上面的,上面虽然能登录,但是如果点击刷新页面,因为访问的地址中增加了/seal,匹配不到前端地址的目录,就会报404错误,当配置上上面配置后,如果匹配不到相应的地址目录,会让其跳转到首页:ElectronicSealManagement/index.html,

因为本身处于登录状态,代码也是相对应的,只要登录不超时,刷新页面就不会变化。

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表