我们遇到了一个问题,如何正确显示使用Nginx服务的角度路由的页面。在我们的角度计划中,我们的基础href是"/dashboard“。该项目有两个不同的路线指向“/仪表板/数据-分析”和“/仪表板/数据-可视化”。
下面是我的基本nginx.conf文件:
server{
listen 80;
server_name localhost;
location /dashboard/ {
alias C:/Users/C123/Desktop/html/dashboard;
index index.html;
try_files $url $url/ /dashboard/index.html;
}
}当我继续使用"localhost/dashboard/“或"localhost/dashboard/data-analysis”时,任何一个页面都将从“C:/ index.html /C 123/Desktop/html/dashboard/”加载"polyfill.js“"runtime.js”"style.js“等,这些页面都是在index.html的<script>标记中定义的。
此外,Nginx试图从“C:/Users/C 123/nginx/nginx-1.16.1/html/”中加载这些文件,而不是我的别名(C:/Users/C 123/Desktop/html/dashboard)。
是否有一种方法可以在“仪表板/”位置块中定义它将读取别名的index.html中的所有内容?我不想在外部定义根,也不想只为".js“文件定义第二个位置块。
更新(此解决方案对我有效):
server{
listen 80;
server_name localhost;
location /dashboard/ {
root C:/Users/C123/Desktop/html/
try_files $uri $uri/ /dashboard/index.html/
}发布于 2020-04-28 13:44:59
您缺少了root指令。
另外,try_files应该尝试加载声明的文件,或者在目录中加载index.html。
没有经过测试,但应该有效:
server {
listen 80;
server_name localhost;
location /dashboard/ {
root C:/Users/C123/Desktop/html;
try_files $uri $uri/index.html =404;
}
}编辑:
如果您试图访问dashboard目录中的文件,而不指定请求中的单词dashboard,则可以为此添加一个要回退的块。
server {
listen 80;
server_name localhost;
# this will serve requests like http://localhost/runtime.js
location / {
root C:/Users/C123/Desktop/html/dashboard;
try_files $uri $uri/index.html =404;
}
# this will serve requests like http://localhost/dashboard/
location /dashboard/ {
root C:/Users/C123/Desktop/html;
try_files $uri $uri/index.html =404;
}
}请记住,当NGINX选择位置上下文时,从前面提到的位置选择将是最长的匹配。
发布于 2021-05-28 12:17:39
您可能会在baseHref和deployUrl中设置angular.json。您可以在projects -> $projectName -> architect -> build ->选项中设置它。
"deployUrl": "/dashboard/",
"baseHref": "/dashboard/",有关这些参数的更多内容,请参见answer。
https://stackoverflow.com/questions/61471102
复制相似问题