【问题标题】:how to change nginx config file for angular application如何更改角度应用程序的 nginx 配置文件
【发布时间】:2021-01-18 20:03:34
【问题描述】:

我的 Angular 应用程序正在 nginx 服务器上运行。当我输入 http://**.8.100.248:30749 它显示 nginx 正在运行。 (显示默认 index.html)。但是我的应用程序存在于html 文件夹中,文件夹名称为test。所以我的应用程序出现在“html -> test-> index.html”;;

所以当我输入 http://**.8.100.248:30749/test/index.html 我的应用程序在浏览器上运行时,但是当我从一个页面路由到另一个页面时,reload 我的页面给了我404

当我重新加载我的角度路由页面时,Nginx 给了我 404。所以我更改了 nginx 配置但仍然没有成功

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    expires -1;
    add_header Pragma "no-cache";
    add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    try_files $uri$args $uri$args/ $uri $uri/ /test/index.html =404;
}

任何更新

【问题讨论】:

  • 哪个 URL 给你 404?你说它无需重新加载就可以工作?然而,这会奏效。 Angular 不是单页应用程序吗?请显示一些网址...
  • @DanFromGermany 以按钮单击为例我打开新组件http://**.8.100.248:30749/test/abc 它显示内容。但是当我重新加载它时它显示 404
  • 因为nginx尝试在abc里面找到index.html但是没有index.html

标签: javascript angular nginx nginx-config


【解决方案1】:

将配置中的root 更改为/usr/share/nginx/html/test 应该可以解决您的问题。

【讨论】:

  • 不使用try_files
  • 可能他不想换根,只是公开使用/test/文件夹。
  • @Joy 你必须弄清楚你真正想要什么。您是否希望 /test/ 文件夹成为 URL 的一部分?
【解决方案2】:

在您的配置文件中添加以下内容:

server {
    listen 80;
    location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html =404;
    }
}

【讨论】:

  • 也许可以修复第一行的缩进。
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2018-04-29
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2016-03-10
  • 2017-02-04
相关资源
最近更新 更多