zoukankan      html  css  js  c++  java
  • 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 进行匹配。

        

  • 相关阅读:
    php测试题整理(0519)
    Ajax调用返回json,xml数据类型(0517--pm)
    python 收集测试日志--格式
    python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax
    appium 下载
    VMware 虚拟机设置固定ip
    Centos7 安装配置 SVN
    【Linux】 Centos7 安装 mysql-8.0
    win7 SP1 64位 原版 百度网盘下载
    win10操作系统 64位 原版 百度网盘下载
  • 原文地址:https://www.cnblogs.com/dee0912/p/4703419.html
Copyright © 2011-2022 走看看