2017-08-31 25 views
0

Nginx將根目錄中的所有靜態內容都服務到根URL。例如,如果根內容位置配置爲/usr/share/nginx/html,其中包含文件/usr/share/nginx/html/foo.html,則url http://localhost/foo.html將提供該html文件。我想在URL中加上上下文路徑的前綴,以便http://localhost/myapp/foo.html應該服務於/usr/share/nginx/html/foo.html文件。爲nginx上的應用添加上下文路徑

我試圖改變位置,並增加一個別名,但給人的404

location/{ 
    root /usr/share/nginx/html; 
    index index.html index.htm; 
    } 

    location /myapp/ { 
    alias /usr/share/nginx/html/; 
    } 

我也希望這樣http://localhost/myapp應該成爲/usr/share/nginx/html/index.html

我使用nginx的的1.12.1-alpine搬運工形象

回答

1

試試這個

index index.html index.htm; 
    location/{ 
    root /usr/share/nginx/html; 
    } 

    location /myapp/ { 
    alias /usr/share/nginx/html/; 
    } 

如果不工作,那麼試試下面

index index.html index.htm; 
    location/{ 
    root /usr/share/nginx/html; 
    } 

    location /myapp/ { 
    alias /usr/share/nginx/html/; 
    try_files $uri $uri/ =404; 
    } 

編輯-1

如果你想讓它沒有尾隨工作/那麼你應該使用下面

location ~ /app(/.*)?$ { 
    alias /usr/share/nginx/html/; 
    try_files $uri $uri/ =404; 
} 
+0

沒有兩個配置爲我工作。 – ares

+0

感謝您的回覆 - 我正在試着用一個斜槓作爲我在問題中寫的。 – ares

+0

配置是100%正確的。我已經在本地進行了測試。我想你還有一些其他的默認配置也可能是你的nginx配置中的問題。添加find的輸出。 -name「* .conf」'在你的'nginx.conf'文件夾中存在的文件夾 –

相關問題