在 nginx 中为位置重写设置重写规则



我完全是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$;

我尝试使用位置指令,但它仅适用于列表中的#2和#3

location / {
        rewrite ^ /index.php?sub=$sub&path=$request_uri;
    }

任何帮助将不胜感激!!

这可能有助于您转发:

server_name ~^(?<sub>.+).domain.com$;
location /images {
    # You can put this code in a snippet file and than include
    # it with e.g. `include assets.conf;` so you can use more
    # folders with longest prefix match blocks, which are more
    # efficient than regex blocks.
    # Tell the browser to cache this for one day
    expires 1d;
    # Just give the resource
    try_files $uri =404;
}
location /index.php {
    # PHP stuff goes here
}
location / {
    rewrite ^ /index.php?sub=$sub&path=$uri last;
}

最新更新