如何为不同的路径配置nginx

时间:2016-08-18 14:54:40

标签: nginx nginx-location

我想在/上配置不同位置的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
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过创建以下内容来实现此目的:

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;
}