Angular Nginx Docker 404 [英] Angular Nginx Docker 404

查看:157
本文介绍了Angular Nginx Docker 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

热衷于尝试解决这个问题.

Been driving myself nuts trying to figure this out.

我有一个非常简单的带有路由的Angular项目,下面是app.module.ts,nginx.conf和docker文件.

I have a very simple Angular project with routing, below is the app.module.ts, nginx.conf and docker file.

我是用集装箱启动的

docker run --rm -d -p 80:80/tcp demo:latest

浏览到 http://localhost ,网站将渲染所有路由

browse to http://localhost and site works all routes render

如果我在 http://localhost/contact 上并刷新页面或直接键入Url,我将从Nginx获取404

If I am on http://localhost/contact and refresh page or type Url in directly I get 404 from Nginx.

nginx.conf包含try_files $ uri/index.html;这是所有存在类似问题的帖子都需要设置的内容.

The nginx.conf has try_files $uri /index.html; this is what all the posts with similar issues have said needs to be set.

知道我在做什么错

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

nginx.conf

server {
    listen   80;

    root /usr/share/nginx/html;
    index index.html;

    server_name _;

    location / {
        try_files $uri /index.html;
    }
}

dockerfile

dockerfile

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

Silly me, I was not overwriting the default docker conf in the docker file

COPY nginx.conf /etc/nginx/conf.d/default.conf

这篇关于Angular Nginx Docker 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆