【发布时间】:2016-09-10 01:17:48
【问题描述】:
我正在尝试部署一个使用框架的路由器功能的 Angular 2,但我在 docker 容器内使用 nginx 为其提供服务时遇到了一些问题。
由 angular-cli 构建的 Angular 应用,文件结构如下:
./dist
├── 08c42df75dd2c636f46f3c564b3ddaa5.ttf
├── 8eab7754b320d3562a2deff1ca81bb6f.woff2
├── assets
│ ├── fonts
│ │ ├── Avenir_Next_New_Regular.otf
│ │ ├── Avenir_Next_New_Regular.ttf
│ │ └── material.woff2
│ └── img
│ ├── angular-2.svg
├── index.html
├── inline.js
├── main.dc3f8a76e21296ab1f32.bundle.js
├── main.dc3f8a76e21296ab1f32.bundle.js.gz
├── styles.771fbb659c3d6c4edd71.bundle.js
└── styles.771fbb659c3d6c4edd71.bundle.js.gz
我正在尝试使用以下 dockerfile 进行部署
FROM nginx:1.10-alpine
COPY dist/ /var/www
COPY ng2.conf /etc/nginx/conf.d/default.conf
CMD 'nginx'
棘手的部分是如何设置下面的default.conf 文件:
server {
listen 80;
root /var/www/;
// The question is how do I set this location block
// making sure both angular and the local files are happy?
location / {
try_files $uri /index.html;
}
}
除了有角度的路线外,一切都有效。含义 / 有效,但 /resume 被重定向到 /
const appRoutes: Routes = [
{
path: '',
component: DevComponent
},
{
path: 'resume',
component: ResumeComponent
}
];
export const router: ModuleWithProviders = RouterModule.forRoot(appRoutes);
【问题讨论】:
-
/resume请求不应命中 nginx。它应该在客户端通过角度处理。你的 nginx 应该只用于处理你的 Angular 应用程序的初始下载。所以我会专注于你的角度设置。你能在这里发布更多内容吗? -
@Alkaline 它可以在本地主机上正常工作。