Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何显示404'的角度6通配符页面;是nginx吗?_Angular_Nginx - Fatal编程技术网

Angular 如何显示404'的角度6通配符页面;是nginx吗?

Angular 如何显示404'的角度6通配符页面;是nginx吗?,angular,nginx,Angular,Nginx,我已经用Angular 6构建了一个应用程序,我正在为404使用Angular 6通配符路由 我使用ngbuild--prod命令将应用程序构建到index.html和一堆静态文件中,并使用nginx为应用程序提供服务。执行此操作时,404页面不起作用。要让nginx使用角度通配符404页面,我需要做一些特殊的事情吗?在运行ng build--prod 编辑 const appRoutes: Routes = [ { path: '', component: LandingComponent

我已经用Angular 6构建了一个应用程序,我正在为404使用Angular 6通配符路由

我使用
ngbuild--prod
命令将应用程序构建到index.html和一堆静态文件中,并使用nginx为应用程序提供服务。执行此操作时,404页面不起作用。要让nginx使用角度通配符404页面,我需要做一些特殊的事情吗?在运行
ng build--prod

编辑

const appRoutes: Routes = [
  { path: '', component: LandingComponent },
  { path: 'changepassword', canActivate: [ AuthGuard ], component: ChangePasswordComponent },
  { path: 'resetpassword', component: ResetPasswordComponent },
  { path: 'resetpassword/:token', component: ResetPasswordPart2Component },
  { path: '**', component: PageNotFoundComponent }
]
编辑

const appRoutes: Routes = [
  { path: '', component: LandingComponent },
  { path: 'changepassword', canActivate: [ AuthGuard ], component: ChangePasswordComponent },
  { path: 'resetpassword', component: ResetPasswordComponent },
  { path: 'resetpassword/:token', component: ResetPasswordPart2Component },
  { path: '**', component: PageNotFoundComponent }
]
为了确认,我理解不应该生成404.html页面

编辑

const appRoutes: Routes = [
  { path: '', component: LandingComponent },
  { path: 'changepassword', canActivate: [ AuthGuard ], component: ChangePasswordComponent },
  { path: 'resetpassword', component: ResetPasswordComponent },
  { path: 'resetpassword/:token', component: ResetPasswordPart2Component },
  { path: '**', component: PageNotFoundComponent }
]
nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}
 http {
    server {
        listen 80;
        server_name  localhost;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;
        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
         location / {
            try_files $uri $uri/ /index.html;
        }
    }
}

这不是它的工作原理

当客户端(浏览器)请求静态资产以外的任何内容时,您应该返回
index.html
页面。此页面将包含一个
标记,用于加载代码——您使用Angular创建的应用程序

这段代码检查URL路径并确定要显示的“页面”(要构建并向最终用户显示的DOM)。如果恰好没有匹配项,并且最后一条路由是通配符,则将显示这样一个“页面”


请注意,从未联系过服务器。为了使Angular的内部路由正常工作,您不需要在nginx上执行任何操作(除了将所有内容重定向到我上面解释的
index.html

server {
    root /usr/share/nginx/html;
    try_files $uri $uri/ index.html;
    error_log /var/log/nginx/angular4_error.log;
    access_log /var/log/nginx/angular4_access.log;
}

我不认为它会生成一个.html页面,因为404组件的内容将在您点击的任何尚未分配的路径上提供。通常情况下,Angular应用程序需要Web服务器配置来通过索引文件路由所有内容。。。您是否为此路由配置了nginx@KyleBurkett我刚刚构建了整个应用程序并将其放入
/usr/share/nginx/html
。除了404之外,应用程序的其余部分工作正常。你能添加nginx配置吗?我想问题可能出在实际的服务器配置中,而不是在你的angular应用程序中。我添加了我的nginx配置,但我并不真的认为它是这样工作的,更多的只是促进讨论。奇怪的是,应用程序部署后返回的404页面是nginx 404页面。然后你没有设置重定向。当用户请求
example.com/profile
时,他应该收到
index.html
文件,而不是404文件,因为服务器上不存在
profile.html
。您是说Angular应用程序中的重定向还是nginx中的重定向?应用程序的其余部分和所有路由在nginx重定向中正常工作。当cliet请求
example.com/profile
时,您当前尝试查找
profile.html
,失败后返回404页。相反,返回
index.html
。我尝试了一些方法,但是
location/{try\u files$uri$uri//index.html;}
似乎是最常用的建议。还是不走运。