Nginx页面不是/ 404与Angular构建包



Angular bundle工作,但是如果我转到另一个页面,例如/cards, nginx返回404。为什么?我使用docker文件来启动nginx, Angular的应用构建使用。/node_modules/@angular/cli/bin/ng.js build我将在谷歌搜索响应,但配置编辑没有帮助

nginx.conf

server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;n
}
}

app.module.ts

...imports...
const appRoutes: Routes =[
{ path: '', component: HomeComponent},
{ path: 'registration', component: RegistrationComponent},
{ path: 'login', component: LoginComponent},
{ path: 'cards', component: CardsComponent},
];

@NgModule({
declarations: [
AppComponent,
HeaderComponent,
HomeComponent,
RegistrationComponent,
LoginComponent,
PreloaderComponent,
CardsComponent,
CardComponent,
CardTextPipe,
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes),
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
构建nginx的Dockerfile
FROM nginx:latest
COPY . ./usr/share/nginx/html
RUN echo "server {n
listen 80;n
server_name localhost;n
root /usr/share/nginx/html;n
index index.html index.htm;n
location / {n
try_files $uri $uri/ /index.html;n
}n
}" > /etc/nginx/conf.d/nginx.conf

问题解决程序,在echo命令中使用类似环境的$uri。

最新更新