Nginx Angular2/Angular路线

Ste*_*ons 12 nginx docker nginx-location angular

我有一个在docker容器内运行的Angular2/Angular应用程序,并使用nginx来提供它.所以我的app base =/myapp /.使用基本网址(即www.server.com/myapp或www.server.com/myapp/)访问应用时,一切正常

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  include /etc/nginx/conf/*.conf;

  server {
    listen       80 default_server;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;
    underscores_in_headers on;

    location /myapp {
      # If you want to enable html5Mode(true) in your angularjs app for pretty URL
      # then all request for your angularJS app will be through index.html
      try_files $uri /myapp/index.html;

    }

    #Static File Caching. All static files with the following extension will be cached for 1 day
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
      expires 1d;
    }

    ## PROXIES ##
    # location matcher for get requests. Any query params will be proxied using this location block
    location = /myapp/api {

      proxy_pass http://$hostname/api$is_args$query_string;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout       120;
      proxy_send_timeout          120;
      proxy_read_timeout          120;
      send_timeout                120;
    }

    # location matcher for post requests i.e. updateAsset. Any request with additional path params will be proxied using this location block
    location ~ ^/myapp/api/(?<section>.*) {

      proxy_pass http://$hostname/api/$section;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout       120;
      proxy_send_timeout          120;
      proxy_read_timeout          120;
      send_timeout                120;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序还有其他几个路由,例如/ myapp/page1或/ myapp/page2.使用nodejs以开发模式提供应用程序时,可以点击这些路径.然而,一旦我将其容器化(容器化不是问题)并使用nginx服务,那么在尝试访问/ myapp/page1或/ myapp/page2时,我找不到404.错误日志输出

2017/02/27 12:15:01 [error] 5#5: *3 open() "/usr/share/nginx/html/myapp/page1" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /myapp/page1 HTTP/1.1", host: "localhost"

我已经尝试在nginx conf文件中映射我的所有应用程序URL但似乎没有任何效果.我如何让它工作?

更新1

增加了角度路线

主应用程序路线:

import { Route } from '@angular/router';
import { MyAppComponent } from './index';

export const MyAppRoutes: Route[] = [
  {
    path: '',
    component: MyAppComponent
  }
];
Run Code Online (Sandbox Code Playgroud)

第1页路线:

import { Route } from '@angular/router';
import { Page1Component } from './index';

export const Page1Routes: Route[] = [
  {
    path:'page1',
    component: Page1Component
  }
];
Run Code Online (Sandbox Code Playgroud)

Dav*_*oal 15

这是我的nginx sites-available/myapp

server {
    listen 80;
    listen 80 [::]:80;
    root /my/root/path;
    server_name www.mysite.com mysite.com;

    location /app1/ {
        alias /my/root/path/app1/;
        try_files $uri$args $uri$args/ $uri/ /app1/index.html;
    }

    location /app2/ {
         ...
    }    
}
Run Code Online (Sandbox Code Playgroud)

设置好配置后,您要启用站点,运行:

sudo ln -s /pathtonginx/sites-available/myapp /pathtonginx/sites-enabled/myapp
Run Code Online (Sandbox Code Playgroud)

我的index.html上的basehref

<base href="./">
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • try_files $ uri $ args $ uri $ args / $ uri / /app1/index.html; 像魅力一样工作 (2认同)

Rae*_*ald 6

所述nginx多克尔图像包括基本配置文件,/etc/nginx/nginx.conf。这个配置文件将各种项目设置为镜像维护者(大概)知道对 Docker 容器来说是好的值的值,因此在为 Angular 提供服务时尽可能多地使用该配置文件是很有诱惑力的。预计用户可能想要这样做,基本配置文件包含指令

http {
...
    include /etc/nginx/conf.d/*.conf;
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以将特定于 Angular 的配置放在一个文件中/etc/nginx/conf.d/ng.conf,并获得使用基本配置文件的好处。

该文件可以很好也很简单。正如Angular 文档Nginx 文档所推荐的那样,它只使用该try_files指令为您index.html的所有深层链接路径提供服务:

  server {
    location / {
      root /usr/share/nginx/html;
      try_files $uri $uri/ /index.html;
    }
  }
Run Code Online (Sandbox Code Playgroud)

并且您的 Dockerfile 只需要将该文件从构建上下文复制到 Docker 映像中:

COPY src/etc/nginx/conf.d/ng.conf /etc/nginx/conf.d/
Run Code Online (Sandbox Code Playgroud)

我这样做了,但没有奏效,因为还有一个障碍。该nginx泊坞窗图像还包括一个文件/etc/nginx/conf.d/default.conf。该文件适用于提供静态内容(来自/usr/share/nginx/html),如果 Nginx 使用该配置优先于您的配置(可能发生),您try_files将毫无用处。因此,您的 Dockerfile 还应删除该默认配置:

RUN rm /etc/nginx/conf.d/default.conf
Run Code Online (Sandbox Code Playgroud)