我正在嘗試通過nginx運行生產構建的Angular應用程式。目前,我可以很好地為 index.html 提供服務,但問題在于索引本身內部作為源參考的所有 *.js 和 *css,它們回傳 404 錯誤。我一直在使用 root 或別名在不同的配置之間來回切換,但似乎沒有一個能解決問題。
首先,我正在使用以下方法構建應用程式:
ng build --configuration production --aot
注意:上面的命令會創建一個dist檔案夾,其中包含運行應用程式所需的所有檔案,然后我將所有這些檔案移動到路徑/apps/customer-ui
檔案夾結構如下:
/apps
/customer-ui
/index.html
/(all other files resulting from build)
這是我的 nginx.conf
...
http {
...
server {
listen 80;
server_name localhost;
root /apps;
index index.html;
location / { }
location ^~/customer-ui {
alias /apps/customer-ui/;
try_files $uri $uri/ /customer-ui/index.html =404;
}
}
}
目前,訪問 http://localhost/customer-ui 會回傳正確的索引,但也會回傳上面提到的 404,因為參考指向的是 http://localhost/filename.js 而不是 http://localhost/customer-ui/檔案名.js
控制臺錯誤在這里
很感謝任何形式的幫助。
uj5u.com熱心網友回復:
由于所有必要的檔案都在customer-ui目錄中,因此最好將所有路徑都指向那里,如下所示:
root /apps/customer-ui;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
或者,您可以使用它(不推薦,因為它相當混亂):
root /apps;
index index.html;
location / {
alias /apps/customer-ui/;
try_files $uri $uri/ index.html =404;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/472768.html
上一篇:我通過`spawn-fcgi`運行`"crash_demo.run"`。如何收集`core`檔案
下一篇:燒瓶重定向導致超時錯誤
