ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

location

2022-01-05 23:31:19  阅读:217  来源: 互联网

标签:opt conf nfs location web01 root


location

目录

location匹配符号

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 3
/ 通用匹配,任何请求都会匹配到 4

初始模板

server {
    listen 80;
    server_name _;  # _ : 表示可以匹配所有域名

    location / {
         default_type text/html; # default_type(默认处理类型)
         return 200 "Hell XJJ"; # return(返回信息)
    }
}

以下展示匹配符号

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 =";
    }
}

此时注意*两者是同级,按顺序匹配。

![截屏2022-01-05 下午7.41.16](/Users/macintoshhd/Desktop/截屏2022-01-05 下午7.41.16.png)

案例

NFS操作
[root@nfs ~]# mkdir /opt/img
[root@nfs ~]# vim /etc/exports
/opt/img  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs ~]# chown -R www.www /opt/img/
[root@nfs ~]# systemctl restart nfs-server rpcbind
[root@nfs ~]# showmount -e
Export list for nfs:
/opt/img  172.16.1.0/20
/web/nfs1 172.16.1.0/20
[root@nfs ~]# 

web01
[root@web01 opt]# cd /opt/
[root@web01 opt]# ll
总用量 0
drwxr-xr-x 2 www  www  329 1月   5 19:56 image
drwxr-xr-x 4 root root  85 1月   5 15:51 linux
drwxr-xr-x 2 root root 104 1月   5 19:57 mairegame
drwxr-xr-x 2 root root   6 1月   2 15:18 nfs
drwxr-xr-x 5 root root  56 1月   4 09:38 xiangqigame

[root@web01 opt]# mkdir image
[root@web01 opt]# mount -t nfs 172.16.1.31:/opt/img /opt/image/
[root@web01 opt]# df -h
[root@web01 opt]# mv mairgame/images/* /opt/image/
[root@web01 opt]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# ll
总用量 12
-rw-r--r-- 1 root root 204 1月   4 23:06 maire.conf.gz
-rw-r--r-- 1 root root 257 1月   5 16:17 py.conf.gz
[root@web01 conf.d]# gzip -d maire.conf.gz (解压)
[root@web01 conf.d]# vim maire.conf 
server {
    listen 80;
    server_name 192.168.15.7;

    location / {
        root /opt/mairgame;
        index index.html;
    }
}

可以看到,进入192.168.15.7无图片加载,此时我们需要通过location将图片重新加载

[root@web01 conf.d]# tail -f /var/log/nginx/access.log
找到"/images/itemsheet.png",此时需要匹配以images开头的路径
[root@web01 conf.d]# cat maire.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    
    location / {
        root /opt/mairegame;
        index index.html;
    }
     # 设置图片路径
     location ~ /images {
        root /opt/image;
    }
}
[root@web01 image]# pwd
/opt/image
[root@web01 image]# mkdir images
[root@web01 image]# mv *.png images/
[root@web01 image]# mv *.gif images/

在web02中只需要要布置mairegame,做下挂载点,就实现图片分离

标签:opt,conf,nfs,location,web01,root
来源: https://www.cnblogs.com/xiejunjie8888/p/15769327.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有