首頁  >  問答  >  主體

伺服器 - 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 天前478

全部回覆(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
  • 取消回覆