【问题标题】:Nginx 404 error when existing urls with Angular 2 routing are refreshed刷新具有 Angular 2 路由的现有 url 时 Nginx 404 错误
【发布时间】:2019-01-29 21:44:31
【问题描述】:

我有一个使用路由的angular 2 应用程序。当我点击我的基本 URL 时,页面会正确加载并路由到下一页。但是,如果我刷新同一页面,则会引发404 error。例如,如果我输入我的基本网址 example.com 然后它会路由到 example.com/dashboard 但如果我刷新此页面则会导致 404 错误。

我的Nginx 配置设置文件中有以下内容:

server  {
  ssl  on;
  ssl_certificate  Certificate.cer;
  ssl_certificate_key  privateKey.key;

  listen  443 ssl;
  server_name example.com;
  location / {
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass  http://localhost/temp/;

 }

 location /app {
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass  http://12.34.56.78:5001;

 }

}

刷新页面时出现404错误。基于this link 我修改了我的Nginx 配置以添加try_files 部分,如下所示:

更新:

server  {
  ssl  on;
  ssl_certificate  Certificate.cer;
  ssl_certificate_key  privateKey.key;

  listen  443 ssl;
  server_name example.com;
  location / {
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass  http://localhost/temp/;
    alias /usr/share/nginx/html/proj1;
    try_files $uri $uri/ /index.html$is_args$args;

 }

 location /app {
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass  http://12.34.56.78:5001;

 }

}

现在它会抛出无法找到JS 文件的错误。我在index.html(例如<script src="app.js"></script>)文件中引用的所有JS 文件都存在于与index.html 相同的目录中。

编辑:这是我在 Chrome 控制台的网络选项卡中看到的 JS 错误之一:

**General:**
Request URL: https://example.com/script.js
Request Method: GET
Status Code: 404 Not Found
Referrer Policy: no-referrer-when-downgrade

**Response Header:**
Connection: keep-alive
Content-Length: 571
Content-Type: text/html
Date:
Server: nginx/1.12.2

【问题讨论】:

    标签: angular nginx


    【解决方案1】:

    当您在例如 http:///some/path 的路径中并且您点击刷新时会出现 404 错误页面时出现问题。 ngnix 配置中的这一额外行可以解决问题。

    server {
      listen 8080;
      server_name http://example.com;
      root /path/to/your/dist/location;
      index index.html index.htm;
      location / {
         try_files $uri $uri/ /index.html?/$request_uri;
        # This will allow you to refresh page in your angular app. 
      }
    }
    

    添加后重新启动 nginx 服务。

    【讨论】:

      【解决方案2】:

      Angular 应用程序是单页应用程序 (SPA)。这意味着您只提供一个 HTML 文件,即 index.html

      如果我刷新:

      /home = index.html

      /fred = index.html

      /any = index.html

      所以你需要告诉 NGINX 总是提供 index.html 而不管路由。

      例如:

      server {
        listen 8080;
        server_name http://example.com;
        root /path/to/your/dist/location;
        # eg. root /home/admin/helloWorld/dist
        index index.html index.htm;
        location / {
          try_files $uri $uri/ /index.html;
          # This will allow you to refresh page in your angular app. Which will not give error 404.
        }
      }
      

      How to config nginx to run angular4 application

      【讨论】:

      • 我尝试添加 try_files 但它现在抛出错误,它无法找到 JS 文件。我在index.html(例如<script src="app.js"></script>)文件中引用的所有JS 文件都存在于与index.html 相同的目录中。在上面的帖子中查看我的更新设置
      • 好的,当浏览器请求一个 .js 文件时,它是否在为索引页提供服务?检查您的浏览器网络选项卡,查看请求的内容和实际交付的内容。如果是这样,您只需要配置 nginx 以将您的新规则仅限于 html 文件。谷歌它,我相信你会找到帮助。如果只是找不到您的 js 文件,那么您可能需要调整根值
      • 我在上面的帖子中添加了我在网络选项卡中看到的一个 JS not found 错误。
      • 好的,所以它只是找不到文件,可能有一个 nginx 属性会告诉它在哪里寻找它们
      • 你知道它可能是什么 nginx 属性吗?
      猜你喜欢
      • 2017-09-09
      • 2017-08-31
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2017-07-20
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多