2012-10-10 112 views
1

我嘗試使用Nginx設置子域,但出現一些錯誤。下面是我的配置:如何設置Nginx配置來重新生成我的子域

server { 
    listen 80; 
    server_name dimain.com *.domain.com; 
    root /path/to/fuelphp_project/public; 

    location/{ 
     index index.php index.html index.htm; 
     if ($host ~ (.*)\.(.*)) { 
       set $sub_name $1; 
       rewrite ^/(.*)$ /index.php/$sub_name/$1 last; 
       break; 
     } 

     if (!-e $request_filename) { 
      rewrite ^.*$ /index.php last; 
      break; 
     } 
    } 

    ... 
} 

我想要的結果,如:

sub1.domain.com/c1/a1 -> domain.com/sub1/c1/a1 
sub2.domain.com/c2/a2 -> domain.com/sub2/c2/a2 

請幫助我如何糾正它。非常感謝!

回答

0
server { 
    server_name sub1.domain.com; 
    return 301 "http://domain.com/sub1${uri}"; 
} 

請問這是否適合您?

我只注意到一個答案在這裏:https://serverfault.com/questions/426673/nginx-redirect-subdomain-to-sub-directory

server { 
    server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub 
    return 301 "http://domain.com/${sub}${uri}"; 
} 

我想正則表達式也可以是一個非常酷的解決方案...取決於你所需要的。

+0

您需要在更改.conf後重新啓動nginx。 – Gabriel

+0

它的工作原理!非常感謝你! – LanceHe

+0

您的歡迎LanceHe – Gabriel