首页 > 解决方案 > nginx -AWS EC2 -Angular 子路径不适用于直接请求

问题描述

我在 AWS ec2 实例上托管了我的 Angular 项目。实例已成功创建,站点正常工作。

什么时候发出请求 www.abc.com 它可以工作但是当我发出请求 www.abc.com/login 它返回 nginx 错误

404 未找到

app.routing.ts

const routes: Routes = [
{
    path: '',
    loadChildren: './layout/layout.module#LayoutModule',
     canActivate: [AuthGuard]
},
{ path: 'signup', loadChildren: './signup/signup.module#SignupModule' },
{ path: 'login', loadChildren: './login/login.module#LoginModule' },
{ path: 'not-found', loadChildren: './not-found/not-found.module#NotFoundModule' },
{ path: '**', redirectTo: 'not-found' },

];

服务器配置 nginx(启用站点)

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/myfolder;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {

                try_files $uri $uri/ =404;
        }

这是角度代码问题还是服务器配置问题?如何解决这个问题?

标签: angularnginxangular5

解决方案


我不得不用 index.html 更改 =404

正确的配置是

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/myfolder;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name www.abc.com;

        location / {

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

推荐阅读