加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx将子域重写为文件作为变量

发布时间:2020-12-13 21:10:05 所属栏目:Nginx 来源:网络整理
导读:如何将子域作为要在重写中添加的变量? hello.example.com应该转到example.com/user.php?u=hello但仍显示为hello.example.com 我目前正在做http://example.com/users/hello,但希望为用户提供Tumblr风格的域名. rewrite ^/users/(.*)$/user.php?u=$1 last; 另

如何将子域作为要在重写中添加的变量?

hello.example.com应该转到example.com/user.php?u=hello但仍显示为hello.example.com

我目前正在做http://example.com/users/hello,但希望为用户提供Tumblr风格的域名.

rewrite ^/users/(.*)$/user.php?u=$1 last;

另外,如果子域名为/ settings /之后的页面为hello.example.com/settings/,我需要重写为settings.php

最佳答案
两件事情.首先,您需要读取主机变量以获取子域并将其设置为稍后使用的变量.我使用了变量$sub_domain,然后将变量附加到URI.

if ($host ~* (.*).example.com) {
     set $sub_domain $1;
     rewrite ^/user.php$/user.php?u=$sub_domain
}

重写行需要进一步调整.我在这里举了一个例子.有关可用选项,请参阅内置的nginx variables documentation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读