2016-01-20 85 views
1

标识静态html网页喜欢为基于URL的用户输入不同的网站。 例如,如果用户将去安装nginx的服务基于URL

my-domain.pl/的内容将从桌面文件送达

my-domain.pl/desktop的内容将从桌面文件夹

my-domain.pl/mobile内容将从送达送达移动文件夹

root 
    | 
    |---mobile 
    |  |--- assets 
    |    |---js,css,img 
    |  
    |---desktop 
     |--- assets 
       |---js,css,img 

我想,在nginx的安装文件:

server { 

    root /desktop 

    location /mobile { 
     root /mobile 
    }  

    location /desktop{ 
     root /desktop 
    }  

} 

,但它仅适用于/路径,其余路径返回404

我试图添加try_files $uri index.html但似乎它返回所有的index.html文件请求这个位置,例如它也会返回index.html文件,而不是JavaScript。

我设立nginx的,所以任何帮助将不胜感激绝对新鲜。

回答

2

您需要使用alias指令代替root:请参阅documentation

server { 

    root /; 

    location /desktop { 
    }  

    location /mobile { 
     alias /mobile; 
    }  
} 

(不要忘了,尾随的分号)

1

您应该避免在location区块内指定root(参见nginx pitfalls)。

你有没有试过如下:

  • 只有一个root
  • 使用rewrite默认

服务/desktop的配置看起来像:

server { 

    root /; 

    ## this should take care of the redirect to /desktop by default: 
    location =/{ 
    rewrite^/desktop/ redirect; 
    } 
    ## this one below probably doesn't work: 
    # rewrite ^/$ /desktop/; 

} 

附:我现在无法使用nginx访问机器,所以我无法检查rewrite语法。另见this answer