在nginx中设置重写规则以进行位置重写

时间:2017-05-15 18:53:52

标签: mod-rewrite nginx url-rewriting

我是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;
    }

任何帮助都将非常感谢!!!

1 个答案:

答案 0 :(得分:1)

这可能会帮助您前进:

float