• Nginx 笔记与总结(7)Location:正则匹配


    在 /usr/local/nginx/conf/nginx.conf 的默认 server 段中,保留默认的 location 信息(之前测试的 location 配置删除):

            location / {
                root   html;
                index  index.html index.htm;
            }
    

     

    在 /var/www 下创建 image 目录:

    [root@localhost ~]# cd /var/www
    [root@localhost www]# mkdir image
    

    使用 wget 或者 ftp 在该目录下下载或者传输一张图片:test.jpg

    修改 /usr/local/nginx/html/index.html,加上 <img src="./image/test.jpg">:

    [root@localhost nginx]# vim html/index.html 
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <img src="./image/test.jpg">
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    

    平滑重启 nginx。

    由于 /usr/local/nginx/html/ 目录下并没有 image 目录,因此访问 192.168.254.100 时,页面上的图片是无法显示的:

      

    直接访问图片地址:

    此时在 location 配置下添加正则匹配的 location 配置(第 2 段),此时的 location 配置为:

            location / {
                root   html;
                index  index.html index.htm;
            }
            
            location ~ image {
                root /var/www/;
                index index.html;
            } 
    

    平滑重启 nginx。

    访问 http://192.168.254.100/image/test.jpg,正则匹配和一般匹配都能匹配上该 uri 的时候,优先正则匹配:

    /var/www/image 目录下的图片显示了。

    直接访问 http://192.168.254.100/image/test.jpg:

    匹配过程是:正则匹配中的 image 能够匹配到 uri 中的 /image/test.jpg,则正则匹配发挥作用,真正访问的是 /var/www/images/test.jpg

    再测试,当多个普通匹配都能匹配到 uri ,哪个匹配能真正发挥作用(比较第 1 段和第 2 段)?

    /usr/local/nginx/conf/nginx.conf:

            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location /foo {
                root   /var/www;
                index  index.htm index.html;
            }
    
            location ~ image {
                root /var/www/;
                index index.html;
            }

    此时 /var/www 目录下有 foo 目录,下面有文件 index.htm : i'm /var/www/index.htm

    对于 uri "/foo",两个 location 的 patt 都能匹配,即 "/" 和 "/foo" 都能左前缀匹配 "/foo",

    访问 http://192.168.254.100/foo:

      

            location /foo {
                root   /var/www;
                index  index.htm index.html;
            }
    

    发挥了作用,原因是 "/foo" 更长,因此使用 "/foo" 的 location 进行匹配。

        

  • 相关阅读:
    Linux04:压缩与解压
    Linux03:基本权限与初始权限
    Linux02:基本命令、查看文件、链接命令
    Linux01:虚拟机配置与系统安装
    分库分表和数据库分片方案
    mysql的锁
    redo log和undo log、事务
    Android : 代码多维度管理(产品风味)
    Android : 网络adb配置及有线端口占用解决方法
    Linux学习: 触摸屏驱动
  • 原文地址:https://www.cnblogs.com/dee0912/p/4703419.html
Copyright © 2020-2023  润新知