【问题标题】:Nginx directory path as argumentsNginx 目录路径作为参数
【发布时间】:2017-09-14 23:19:24
【问题描述】:

我正在尝试使用 nginx 中的 location 块将目录路径作为 get 参数传递到 index.php 中。

所以 url /api/arg1/arg2/ 要作为 /api/index.php?arg1=$1&$arg=2 传入

这是我在下面的尝试,有人可以帮助使用正则表达式或更好的方法。

location ~ "^/api/(\w+)/([A-z,0-9].*)/$" {

    alias /api/index.php?arg1=$1&arg2=$2;

}

旁注,要传递的参数是一个单词,没有由数字和字母组成的空格,不需要其他符号。

【问题讨论】:

    标签: regex nginx url-rewriting


    【解决方案1】:

    试试下面

    location ~ /api/(?P<arg1>\w+)/(?P<arg2>[A-z,0-9].*)/$" {
    
        rewrite "^.*$" /api/index.php?arg1=$arg1&arg2=$arg2;
    
    }
    

    如果您不担心这些 args 中的数据,我宁愿使用

    location ~ "/api/(?P<arg1>[^/]+)/(?P<arg2>[^/]+)/$" {
    
        rewrite "^.*$" /api/index.php?arg1=$arg1&arg2=$arg2;
    
    }
    

    【讨论】:

    • 感谢您的回复。在使用 nginx 并获得重定向循环、找不到文件和配置错误大约五个小时后,我放弃并尝试了 nodejs,并在三分钟后设法得到了我正在尝试的东西。 app.get("/api", handle);app.get(/^\/api\/([a-z]+)\/$/, handle);app.get(/^\/api\/([a-z]+)\/([a-z,0-9]+)\/$/, handle);
    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 2013-12-07
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多