Angular Nginx Docker 404

标签 angular docker nginx http-status-code-404

一直在努力解决这个问题。

我有一个非常简单的带有路由的 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/

相关文章:

python - Nginx 和 CSS 问题

javascript - md-list-item 内的 md-radio-button

angular - 何时使用 FormGroup 与 FormArray?

javascript - Angular 2 - 如何在等待 http 请求完成时阻止 GUI 渲染

docker - 如何从 docker hub 上的私有(private) docker 存储库中提取?

mysql - Node/Redis/Socket/pm2 服务器在约 11 小时后关闭连接

javascript - ionic 本地通知删除对话框

linux - 这个奇怪的 docker 命令是什么意思?

docker - concourse 无法放入私有(private) docker 注册表,重试直到出现 500 错误

python - 每当我更改代码时,Flask 都会将所有人注销