【发布时间】:2017-05-24 07:16:29
【问题描述】:
我完全是 nginx 的菜鸟。我正在尝试在 nginx 中为位置重写设置一些规则。我想要实现的是:
1. no change
https://hello.domain.com/index.php --> https://hello.domain.com/index.php
2. subdomain needs to be included
https://hello.domain.com --> https://hello.domain.com/index.php?sub=hello
3. subdomain & folder structure needs to be included
https://hello.domain.com/dir1/15 --> https://hello.domain.com/index.php?sub=hello&path=dir1/15
4. requests to certain directories ( images, css, etc... ) need to be ignored
https://hello.domain.com/images/logo.png --> https://hello.domain.com/images/logo.png
我可以使用这个将子域放入变量 $sub
server_name ~^(?<sub>.+)\.domain\.com$;
我尝试使用 location 指令,但它仅适用于列表中的 #2 和 #3
location / {
rewrite ^ /index.php?sub=$sub&path=$request_uri;
}
任何帮助将不胜感激!!!
【问题讨论】:
-
您可以使用
try_files处理案例#4,请参阅this answer 以获得指导。 -
或者这个:
if ($request_uri !~ "^/(?:images|css)") { rewrite ^ "/index.php?path=$request_uri&sub=$sub" last; } -
我一直在读到不推荐使用
if条件句 -
这也适用于
location范围,因为会进行内部重定向
标签: mod-rewrite nginx url-rewriting