【发布时间】:2019-10-12 13:23:31
【问题描述】:
一直在让自己发疯,试图弄清楚这一点。
我有一个非常简单的带有路由的 Angular 项目,下面是 app.module.ts、nginx.conf 和 docker 文件。
我通过容器启动
docker run --rm -d -p 80:80/tcp demo:latest
浏览到http://localhost,网站可以渲染所有路由
如果我在 http://localhost/contact 并刷新页面或直接输入 Url,我会从 Nginx 得到 404。
nginx.conf 中有 try_files $uri /index.html;这是所有有类似问题的帖子都说需要设置的。
知道我做错了什么。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ContactComponent } from './contact/contact.component';
import { AboutComponent } from './about/about.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'about', component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
nginx.conf
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
server_name _;
location / {
try_files $uri /index.html;
}
}
码头文件
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/
RUN rm -rf /usr/share/nginx/html/*
COPY ./dist /usr/share/nginx/html
【问题讨论】:
标签: angular docker nginx http-status-code-404