繁体   English   中英

减少Nginx重写规则

[英]reducing a nginx rewrite rule

PHP MVC样式的路由器通常处理诸如index.php?route=one/two/three...的请求-用正斜杠分隔的查询字符串为系统提供了要遵循的路由(例如,文件夹>文件>函数,或者公司>部门>程序>行动)。

一个示例请求将是:

GET http://localdomain/user/profile/modify
  > /index.php?route=user/profile/modify
    > /user/profile.php
      > modify()

考虑到所有这些,我编写了如下的nginx服务器配置:

set $i /index.php;
location ~ ^/([a-z0-9-]*)$                             { rewrite ^/([a-z0-9-]*)$                             $i?route=$1       last; }
location ~ ^/([a-z0-9-]*)/?([a-z0-9-]*)$               { rewrite ^/([a-z0-9-]*)/?([a-z0-9-]*)$               $i?route=$1/$2    last; }
location ~ ^/([a-z0-9-]*)/?([a-z0-9-]*)/?([a-z0-9-]*)$ { rewrite ^/([a-z0-9-]*)/?([a-z0-9-]*)/?([a-z0-9-]*)$ $i?route=$1/$2/$3 last; }

正则表达式最佳实践让我大吃一惊,将其更改为能够处理1个或多个参数(可能使用中继器)的一行。 我的正则表达式知识有限,我该怎么办?

不管文件夹结构是通过在字符组中添加斜线来实现多长时间,它都会向index.php?route=发送任何请求:

location ~ ^/([a-z0-9\/-]*)$ { rewrite ^/([a-z0-9\/-]*)$ $i?route=$1 last; }

它基本上可以处理文件夹类型的请求,并且不会在带有点的任何内容上触发(例如.php .html等),因此index.php和其他文件是安全的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM