AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1158681
Accepted
glenatron
glenatron
Asked: 2024-04-30 08:13:32 +0800 CST2024-04-30 08:13:32 +0800 CST 2024-04-30 08:13:32 +0800 CST

让 nginx 将所有路由传递给 Angular index.html

  • 772

我有一个托管在 nginx 上的 Angular 应用程序,它与后端 API 进行通信。后端正在工作,当我访问时应用程序正在工作example.com/,但如果我访问example.com/custom/path/123它不会被路由到index.html,这是 Angular 页面。相反,我只得到一个 nginx 404 页面。

我真正想要的是将任何传入路由(除了/api和/assets路径)传递到该索引。

在我的配置中我有这个:

server {
  root /var/www/example.com/site-root;

  server-name example.com;

  location / {
     try_files $uri $uri/ index.html;
     # I also tried:
     # try_files $uri$args $uri$args/ index.html;

  } 

  location /assets {
     try_files $uri =404;
  }

  location ~ ^/api/(.*)$ {
      proxy_pass http://127.0.0.1:3322/$1;
      # a bunch of working api reverse proxy that doesn't seem to be 
  }
}

该try_files方法是我在许多示例中看​​到的模式,它似乎对其他人都有效,我使用它是错误的还是在这种情况下这是错误的方法?

nginx
  • 1 1 个回答
  • 54 Views

1 个回答

  • Voted
  1. Best Answer
    Alexey Ten
    2024-04-30T15:54:56+08:002024-04-30T15:54:56+08:00

    的最后一部分try_files是内部重定向的 URI,它应该以 开头/。所以正确的用法是try_files $uri $uri/ /index.html;。

    但在这种情况下,您可能需要将所有内容重定向到/index.html而不尝试查找其他文件。所以你可以只使用rewrite.

    你也不需要try_files $uri =404。这就是 nginx 默认会做的事情。

    如果可以在没有正则表达式的情况下完成,则应避免在位置中使用正则表达式。

    所以我认为这就是你得到的:

    server {
      server-name example.com;
    
      root /var/www/example.com/site-root;
    
      location / {
         rewrite ^ /index.html break;
      } 
    
      location /assets/ {
      }
    
      location /api/ {
          proxy_pass http://127.0.0.1:3322/;
      }
    }
    
    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve