首页  >  问答  >  正文

服务器 - nginx怎么把目录设为二级域名?

假设域名为 domain.com,

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

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

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

全部回复(3)我来回复

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 17:17:55

    关键部分:

    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 /
        {
            #一些配置
        }
    }

    回复
    0
  • 高洛峰

    高洛峰2017-05-16 17:17:55

    雷雷

    回复
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 17:17:55

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

    回复
    0
  • 取消回复