Angular 如何使用nginx从子目录提供单页应用程序?

Angular 如何使用nginx从子目录提供单页应用程序?,angular,nginx,Angular,Nginx,我试图将nginx配置为从应用程序特有的子路径提供角度应用程序。我已经修改了应用程序以使用基本hreffish,并且我可以正确地为根页面及其资产提供服务 我无法在子路由上重新加载页面。我有一个404 user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; }

我试图将nginx配置为从应用程序特有的子路径提供角度应用程序。我已经修改了应用程序以使用基本href
fish
,并且我可以正确地为根页面及其资产提供服务

我无法在子路由上重新加载页面。我有一个404

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
  worker_connections  1024;
}


http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$request_uri --- $remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  keepalive_timeout  65;


    upstream docker-backend-stream {
      server fish-api:80;
    }

    server {
      listen 80;
      autoindex off;

      location / {
            rewrite ^ http://www.google.com redirect;
      }

      location /fish {
        alias /app;
        index index.html;
        try_files $uri $uri/ /index.html;
      }

      location /fish/api {
            proxy_pass http://docker-backend-stream;
            auth_basic          off;
            client_max_body_size  0;
        }
    }
}
为什么对index.html不提供GET请求?它收到一个404未找到。

日志:


如上所示,location指令不匹配,因此nginx正在默认根位置/etc/nginx/html中查找资产

似乎有一个历史上的nginx错误,它阻止root和alias正确地掌握location指令

我的变通方法使用条件检查,然后是正则表达式和重写:

  location /fish {
    alias /app;
    if (!-e $request_filename) {
      rewrite ^[^.]*$ /fish/index.html last;
    }
  }

注意:检查文件扩展名肯定会有所改进。
try\u files
语句的最后一个元素应该是URI或响应代码。使用
/fish/index.html
,因为这是正确的URI。请参阅@RichardSmith,即使使用/app/index.html也可以在全局根目录中设置其外观。我认为这与
  location /fish {
    alias /app;
    if (!-e $request_filename) {
      rewrite ^[^.]*$ /fish/index.html last;
    }
  }