美文网首页
nginx中路径的匹配问题

nginx中路径的匹配问题

作者: 有时右逝 | 来源:发表于2020-03-06 16:41 被阅读0次

前言

与nginx的打交道很多,经常遇到404问题,每次出现都需要查资料才能写出结果。这次来总结一下。

问题

访问的域名是 localhost/store/add_order.html
web根目录是/www/web
store是子目录。

解决

配置如下



server {
    listen       80;
    server_name  localhost;
    root   /www/web;
    #charset koi8-r;
    location / {
        index  index.php index.html index.htm;
    }


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

location ^~ /api/
{ 
    proxy_pass http://www.api.com/; 
}



    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


以上location存在3个匹配规则

最先匹配 location = /50x.html
再次匹配 location ~ .php$
最后 匹配 /

例如 http://localhost/store/add-order.html

将去 /www/web 目录下寻找 store/add-order.html 文件。

例如 http://localhost/store/add-order.php

将 /www/web 目录下和 store/add-order.php 组合成 SCRIPT_FILENAME,传递给php组件。

代理

例如 http://localhost/api/add-order.php

则代理成 http://www.api.com/add-order.php

但是如果 没有写/ 结尾,
proxy_pass http://www.api.com;
则代理成 http://www.api.com/api/add-order.php

补充

location用于匹配url
它进行的是模糊匹配
没有“/”时,location /abc/def可以匹配/abc/defghi请求,也可以匹配/abc/def/ghi等
而有“/”时,location /abc/def/不能匹配/abc/defghi请求,只能匹配/abc/def/anything这样的请求

扩展

location = / {
   #规则A
}
location = /login {
   #规则B
}
location ^~ /static/ {
   #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #规则D,注意:是根据括号内的大小写进行匹配。括号内全是小写,只匹配小写
}
location ~* \.png$ {
   #规则E
}
location !~ \.xhtml$ {
   #规则F
}
location !~* \.xhtml$ {
   #规则G
}
location / {
   #规则H
}

那么产生的效果如下:

访问根目录/, 比如http://localhost/ 将匹配规则A

访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H

访问 http://localhost/static/a.html 将匹配规则C

访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C

访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。

访问 http://localhost/a.xhtml 不会匹配规则F和规则G,

http://localhost/a.XHTML不会匹配规则G,(因为!)。规则F,规则G属于排除法,符合匹配规则也不会匹配到,所以想想看实际应用中哪里会用到。

访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

优先级:(location = ) > (location 完整路径 ) > (location ^~ 路径) > (location ~,~* 从上向下正则顺序,匹配在最后一条终止) > (location 部分起始路径) > (/)

相关文章

  • nginx中路径的匹配问题

    前言 与nginx的打交道很多,经常遇到404问题,每次出现都需要查资料才能写出结果。这次来总结一下。 问题 访问...

  • Nginx配置proxy_pass转发包含当前location问

    在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,...

  • Nginx 路径匹配规则,通配符

    Location 语法规则 语法规则: location [=|~ |~ *|^~] /uri/ { … } = ...

  • Nginx location的正则匹配

    Nginx location的正则匹配 Nginx正则匹配的匹配规则

  • Nginx中的location规则补充

    这是一篇关于Nginx中Location规则的补充说明。重点在于测试路径规则和^~规则以及正则规则同时匹配的情况,...

  • NGINX 正则

    Nginx中,set $para $1,$1表示路径中正则表达式匹配的第一个参数。 以下是一个示例,用以实验$1,...

  • 如何利用Nginx实现路径匹配

    首先,假设我们已经有一台可用的Nginx,这个时候,如果我们需要专门匹配用户端的带某一特殊路径的请求,并转发到服务...

  • 91104-nginx failed(13:Permission

    问题背景 nginx 配置好以后 发现请求不能转发 问题分析 查看Nginx 日志,Linux 日志默认路径 ht...

  • glob表达式

    glob表达式(glob expressions)通配符: * 匹配文件路径中的0个或多个字符,但**不会匹配路径...

  • docker部署nginx

    1.docker中默认路径 conf /etc/nginx/nginx.conf html /usr/...

网友评论

      本文标题:nginx中路径的匹配问题

      本文链接:https://www.haomeiwen.com/subject/euoirhtx.html