2016-08-18 144 views
0

我想配置nginx的不同位置在/上,我有以下配置。如何爲不同路徑配置nginx

server { 
    listen 80; 
    server_name bla.com; 
    location =/{ // on exact route match such as 'bla.com' 
     root /usr/share/nginx/html/folder; // it has index.html 
    } 
    location/{ // rest all url such as bla.com/* are captured here eg. bla.com/temp/12 
     root /usr/share/nginx/html/dist; // different folder 
    } 
} 

我該怎麼做?

我也嘗試了以下配置,沒有運氣 -

server { 
    listen 80; 
    server_name bla.com; 
    root /usr/share/nginx/html; 
    location =/{ 
     try_files $uri /folder/index.html; // it has index.html 
    } 
    location/{ // rest all url such as bla.com/* are captured here eg. bla.com/temp/12 
     try_files $uri /dist/index.html; // different folder 
    } 
} 
+0

我真的不知道你在'/'上的不同位置是什麼意思。你的意思是說你的域名根目錄有兩個不同的網站?因爲我認爲這是不可能的(至少在NGINX配置文件中是不可能的)。 – StackerStan

+0

我的意思是,如果我給bla.com我想要去一個位置和bla.com/temp到其他位置 –

回答

0

你可以做到這一點通過創造這樣的事情:

server { 

    index index.html index.htm; 
    server_name test.example.com; 

    location/{ 
     root /var/www/website1/public_html; 
    } 

    location /temp { 
     root /var/www/website2/public_html; 
    } 
} 

您也可以使用這樣的單個文件做到這一點:

location /robots.txt { 
    alias /var/www/website/public_html/robots.txt; 
} 
+0

temp是一個變量在這裏......我很抱歉,我應該更好地解釋。基本上temp可以是任何路徑。我希望我現在有道理 –

+0

好的,那麼這個答案:http://serverfault.com/questions/656628/nginx-catch-all-other-locations-than-given可能會幫助你。 – StackerStan

+0

即使有提到的答案 - '我認爲它可能是使用位置= /。顯式/請求將被匹配,其他所有內容都將到達位置/除非其他匹配項「不起作用,這正是我想要弄清的 –