2016-08-18 143 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

即使有提到的答案 - '我认为它可能是使用位置= /。显式/请求将被匹配,其他所有内容都将到达位置/除非其他匹配项“不起作用,这正是我想要弄清的 –