求助: Nginx 配置 location 问题

131 天前
 qize0921

由于有个参数是数字,还是动态的,所以我的预期是想达到类似下面这种(随便写的):

location /test/(\d+)/ {
    	alias   /www/demo/;
        index  index.html;
}

目前情况是,不能通过 ? 拼接参数,所以只能放到 url 里面了。然后网上找了很多例子也自己调试了很多次,都是无法生效。有没有懂的大佬,麻烦指点一下,谢谢!

1110 次点击
所在节点    NGINX
7 条回复
edotac
131 天前
Nginx 这么配置
```
location /test/ {
alias /www/demo/;
index index.html;
}
```
在前端项目中设置 id 的路由匹配,比如 vue 中的 vuerouter
vampuke
131 天前
location ~ ^/test/(\d+)/$
beyondstars
131 天前
要通过正则表达式匹配路径,location 后面应以 ~ 或者 ~* 开头,其中 ~* 是 case-insensitive 的(不区分大小写)。

试试

```
location ~ ^/test/(\d+)$ {
# 你的其它内容
}
```

详见

- 官方 doc: https://nginx.org/en/docs/http/ngx_http_core_module.html#location
- nginx 匹配测试: https://nginx.viraptor.info/
qize0921
131 天前
@vampuke
@beyondstars

我看两位给的解决方案都差不多,但是我都测试了下,访问还是 404 ,无法匹配到路径,但是我去掉正则,就单匹配 /test/就正常了。不知道是哪里的原因。
qize0921
131 天前
@edotac 就一个单页面,没有用到框架
coolloves
130 天前
/www/demo 去掉/试试
julyclyde
128 天前
@qize0921 可以开 error_log 详细级别看一下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1004466

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX