【问题标题】:Multiple angular 2 apps and /api on same host with nginx使用 nginx 在同一主机上的多个 angular 2 应用程序和 /api
【发布时间】:2026-02-10 08:15:02
【问题描述】:

我正在尝试通过 nginx 配置来实现这个目标:

mydomain.com/api        should serve the php API
mydomain.com/login      should serve an angular 2 app
mydomain.com/dashboard  should serve another angular 2 app

我在下面发布的 nginx.conf 工作正常,除了这个(这是我需要帮助解决的问题):

当用户访问像 mydomain.com/dashboard/forms 这样的 Angular 路由时,URL 会像这样重写 mydomain.com/dashboard/dist/forms

这是代码树:

├── mydomain.com
│   ├── app           // slim project
│   │   └── ...
│   ├── public
│   │   ├── dashboard // anuglar project
|   |   |   └── dist
│   │   └── login     // angular project
|   |       └── dist

这是我使用的服务器块:

server {

  listen 80;
  server_name mydomain.com;

  error_log /home/test/code/mydomain.com/error.log;
  access_log /home/test/code/mydomain.com/access.log;
  root /home/test/code/mydomain.com/public/;

  sendfile on;
  rewrite_log on;
  include  /etc/nginx/mime.types;

  gzip on;

  location ~ ^/(assets|bower_components|scripts|styles|views) {
    expires     31d;
    add_header  Cache-Control public;
  }

  ############################################################################
  #                             D A S H B O A R D
  ############################################################################

  location /dashboard/ {
    alias /home/test/code/mydomain.com/public/dashboard/dist/;
    try_files $uri $uri/ /index.html =404;
  }

  ############################################################################
  #                                L O G I N
  ############################################################################

  location /login/ {
    alias /home/test/code/mydomain.com/public/login/dist/;
    try_files $uri $uri/ /index.html =404;
  }

  ############################################################################
  #                                 A P I
  ############################################################################

  location ~ ^/api/(.*)$ {
    alias /home/test/code/mydomain.com/public;
    try_files $1 $1/ @php;
    index index.php;
  }

  location @php {
    fastcgi_split_path_info ^(/api)(/.*)$;
    fastcgi_pass localhost:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /home/test/code/mydomain.com/public/index.php;
    fastcgi_param REQUEST_URI $fastcgi_path_info;
    fastcgi_read_timeout 900;
  }
}

我应该如何更改以使“/dist”不会写入 url?

【问题讨论】:

    标签: angular nginx config


    【解决方案1】:

    配置文件其实没问题。问题是使用 --base-ref /dashboard/dist/ 而不是普通的 /dashboard/ 构建 Angular 应用程序

    【讨论】:

      最近更新 更多