一直在努力解决这个问题。
我有一个非常简单的带有路由的 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 {}
配置文件
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
最佳答案
傻我,我没有覆盖docker文件中的默认docker conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
关于 Angular Nginx Docker 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318079/