简体   繁体   English

带变量的Nginx重写规则

[英]Nginx rewrite rule with variable

I would like to send all request that came from a sub-domain to index.php and send subdomain name as a $sub 我想将来自子域的所有请求发送到index.php并将子域名作为$sub发送

for example: 例如:

sub1.mydomain.com
send the above request to:
/var/www/mydomain/index.php?sub=sub1

what I've tried: 我尝试过的

rewrite ^(\w+).mydomain.com /index.php?sub=$1 last;
root /var/www/mydomain/;

Rewrites are done in the request URI. 重写是在请求URI中完成的。 This does not contain the host name. 不包含主机名。 Assign the variable sub a default, then using an id statement capture the subdomain replacing the default. 为变量sub分配默认值,然后使用id语句捕获替换默认值的子域。 Then use an unconditional rewrite. 然后使用无条件重写。

$sub=www;
if($host ~ ^(\w+\.example.com$) {
    $sub = $1;
}
rewrite ^.*$ /index.php?sub=$sub$args;

From the top of my head. 从我的头顶。 Tweak it using the relevant documentation. 使用相关文档进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM