我正在尝试部署一个使用框架的路由器功能的 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);