Nginx的location

一:location详解

1.简介
使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分。

二:location匹配规则

Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理)。location支持正则表达式,配置十分灵活。我们可以在一个虚拟主机(nginx中的一个server节点)下配置多个location以满足如动静分离,防盗链等需求。
1.location匹配符号
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 3
/ 通用匹配,任何请求都会匹配到 4

image

    1. 先进行精准匹配,如果匹配成功,立即返回结果并结束匹配过程。
    1. 进行普通匹配,如果有多个location匹配成功,将“最长前缀”的location作为临时结果(如果是 ^~类型的普通匹配成功则直接返回结果,结束匹配过程)。
    1. 由上至下逐一进行正则匹配,一旦匹配成功1个,立即返回结果,并结束解析过程;如果没有一个正则匹配成功,那么将普通匹配的最长前缀location作为最终结果返回,并结束匹配过程。
    1. 实际使用建议

三:location匹配符号案例


server {
    listen 80;
    server_name _;
	
	不区分大小写的正则匹配
    location ~* /python {
        default_type text/html;
        return 200 "Location ~*";
    }
	
	区分大小写的正则匹配
    location ~ /Python {
        default_type text/html;
        return 200 "Location ~";
    }
	以某个字符串开头             
    location ^~ /python {
        default_type text/html;
        return 200 "Location ^~";
    }
	
	精确匹配
    location = /python {
        default_type text/html;
        return 200 "Location =";
    }
}

四:匹配符号超级玛丽共享(图片数据)案例

超级玛丽游戏软件

需求:
1.将超级玛丽游戏(images)内游戏图片配置文件设置成共享数据
2.将多个web服务器共享数据(images)图片数据
1.nfs服务器(共享数据)
2.创建挂载点(挂载的目录)
[root@nfs ~]# mkdir /opt/img
[root@nfs ~]# cd /opt/
3.配置挂载点
[root@nfs opt]# vim /etc/exports

增加挂载点
/opt/img        172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
4.给挂载点授权
[root@nfs opt]# chown -R www.www /opt/img/
5.重启
[root@nfs opt]# systemctl restart nfs-server rpcbind
6.检查服务端是否正常
[root@nfs opt]# showmount -e

Export list for nfs:
/opt/img    172.16.1.0/20
/web/upload 172.16.1.0/20
/web/nfs1   172.16.1.0/20
五:web服务器(共享数据操作)
1.创建目录
[root@web01 opt]# mkdir image
2.挂载NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/opt/img /opt/image
3.查看挂载点
[root@web01 opt]# df -h

image

4.将马里奥内的图片文件移动到 数据共享文件内
[root@web01 opt]# mv html5-mario/images/* /opt/image/

image

5.配置文件
[root@web01 conf.d]# vim game1.conf

image

6.测试
nginx -t
7.重启
systemctl restart nginx
8.网址测试
192.168.15.7
网址不显示文件,因图片被移动到/opt/image文件内

image

9.网址模块修改
[root@web01 ~]# vim /etc/nginx/conf.d/game1.conf

指定马里奥游戏图片 使用图片配置文件路径(共享路径)

image

server {
    listen 80;
    server_name 192.168.15.7;

    location / {
         root /opt/html5-mario;
         index index.html;
    }

    location ~ /image {
        root /opt/image;
    }
}
6.测试
nginx -t
7.重启
systemctl restart nginx
8.网址测试
测试结果不显示图片,找寻原因,查看日志
192.168.15.7

image
image

9.日志实时监控 查询错误日志
日志实时监控 查询错误日志
	解决方法如下:
	
[root@web01 ~]# tail -f /var/log/nginx/error.log

image

10.解决方案
解决:

[root@web01 ~]# cd /opt/image/
创建文件
[root@web01 image]# mkdir images
将原文件内图片配置移动到该目录下
[root@web01 image]# mv *.png images/
[root@web01 image]# mv *.gif images/

image

11.网址测试
192.168.15.7
实现图片共享成功

image

六:多站共存(共享数据)

  • web02服务器
1.web01传输给另一个客户端
scp html5-mario 172.16.1.8:/opt/
2.web02服务器
解压
[root@web02 ~]# unzip html5-mario.zip
3.创建目录
[root@web02 opt]# mkdir image
4.挂载NFS
[root@web02 opt]# mount -t nfs 172.16.1.31:/opt/img /opt/image
5.查看挂载点
[root@web02 opt]# df -h

image

6.修改网址模块配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/game.conf

image

7.测试
nginx -t
8.重启
systemctl restart nginx
9.网址测试
192.168.15.8
成功(多web服务器数据共享)

image

posted @ 2022-01-05 21:41  AlexEvans  阅读(340)  评论(1编辑  收藏  举报