2016-01-20 83 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