服务器 - nginx怎么把目录设为二级域名?
巴扎黑
巴扎黑 2017-05-16 17:15:55
[Linux讨论组]

假设域名为 domain.com,

怎么配置 nginx.conf, 实现 访问 abc.domain.com 的时候实际是 domain.com/abc的内容
(浏览器的地址仍然显示成abc.domain.com )

在网上搜的这个配置没有用:

location / { 
    set $domain default; 
    if ( $http_host ~* "^(.*)\.domain\.com$") { 
                  set $domain $1; 
    } 
    rewrite ^/(.*)    /$domain/$1 last; 
}
巴扎黑
巴扎黑

全部回复(3)
过去多啦不再A梦

关键部分:

server
{
    listen   80;
    server_name   www.domain.com;
    root   /var/www/html;
    index  index.php index.html index.htm;

    location /abc
    {
        if ( $host !~* "abc.domain.com" )
        {
            return 404;  #防止有人访问www.domain.com/abc看到abc二级域名的页面,只允许访问abc.domain.com查看
        }
    }
}

server
{
    listen   80;
    server_name   abc.domain.com;
    root   /var/www/html/abc;
    index  index.php index.html index.htm;

    location /
    {
        #一些配置
    }
}
高洛峰
location / { 
    #申明domain变量 
    set $domain default;
    #正则提取出二级域名
    if ( $http_host ~* "^(.*)\.domain\.com$") { 
        #把二级域名赋值给domain变量
        set $domain $1; 
        #设置rewrite规则,把/下的所有请求转发到http://domain.com/$domain/下。
        rewrite ^/(.*)$    http://domain.com/$domain/$1 last; 
    } 

}
phpcn_u1582

另外建一个server不就行了嘛!还搞那么复杂,又是正则又是rewrite的。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号