使用Nginx在同一主机上的多个angular 2应用程序和/api [英] Multiple angular 2 apps and /api on same host with nginx

查看:88
本文介绍了使用Nginx在同一主机上的多个angular 2应用程序和/api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过nginx配置实现此目标:

I'm trying to achieve this goal with nginx configuration:

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可以正常工作,除了这个(这是我需要帮助解决的问题):

The nginx.conf I post below works fine except for this (which is the problem i need help fixing):

当用户以诸如 mydomain.com/dashboard/forms 之类的角度访问路线时,URL就会像这样 mydomain.com/dashboard/dist/forms

When the user visits a route in angular like mydomain.com/dashboard/forms then the url is rewritten like this mydomain.com/dashboard/dist/forms

这是代码树:

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

这是我使用的服务器块:

And here is the server block I use:

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;
  }
}

我应该更改什么,以免在URL中写入"/dist"?

What should I change so that the "/dist" doesn't get written in the url?

推荐答案

配置文件实际上很好.问题是使用--base-ref/dashboard/dist/而不是普通的/dashboard/

The configuration file is actually fine. The problem was building the angular app with --base-ref /dashboard/dist/ instead of plain /dashboard/

这篇关于使用Nginx在同一主机上的多个angular 2应用程序和/api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆