-4
  1. The problem is when directly redirecting to http://localhost:4200/about working fine but after pushing into nginx server, when I try to redirect to www.webpage.com/about getting error(nginx 404 error). But its working fine when you redirect to www.webpage.com and then click on about. Getting error only I am trying to open direct url as www.webpage.com/about or www.webpage.com/contact or www.webpage.com/blog

    import { NgModule } from '@angular/core';
    import { HttpModule } from '@angular/http';
    import { RouterModule } from "@angular/router";
    import { BrowserModule } from '@angular/platform-browser';
    import { AppRoutingModule } from './app-routing.module';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    import { appRoutes } from "./app-routing.module";
    
    import { HomeComponent } from './home/home.component';
    import { AboutComponent } from './about/about.component';
    import { BlogComponent } from './blog/blog.component';
    import { ClientsComponent } from './clients/clients.component';
    import { ContactComponent } from './contact/contact.component';
    import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
    
    
    
    @NgModule({
        declarations: [
            AppComponent,
            HomeComponent,
            AboutComponent,
            BlogComponent,
            ClientsComponent,
            ContactComponent,
            PageNotFoundComponent
        ],
        imports: [
            BrowserModule,
            AppRoutingModule,
            HttpModule,
            FormsModule,
            ReactiveFormsModule,
    
            RouterModule.forRoot(appRoutes)
        ],
        providers: [
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    
    
     import { NgModule } from '@angular/core';
        import { RouterModule, Routes } from '@angular/router';
    
        import { HomeComponent } from './home/home.component';
        import { AboutComponent } from './about/about.component';
    
        import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
        import { ClientsComponent } from './clients/clients.component';
    
        import { ContactComponent } from './contact/contact.component';
        import { BlogComponent } from './blog/blog.component';
    
    
    
    
        export const appRoutes: Routes = [
            { path: '', redirectTo: '/', pathMatch: 'full' },
            { path: '', component: HomeComponent },
            { path: 'about', component: AboutComponent },
            { path: 'clients', component: ClientsComponent },
            { path: 'contact', component: ContactComponent },
            { path: 'blog', component: BlogComponent },
            { path: 'page-not-found', component: PageNotFoundComponent },
            { path: '**', component: HomeComponent }
        ];
    
        @NgModule({
            imports: [
                RouterModule.forRoot(appRoutes)
            ],
            exports: [
                RouterModule
            ]
        })
        export class AppRoutingModule {
        }
    
Boulboulouboule
  • 4,087
  • 1
  • 13
  • 29
Srinivas
  • 1
  • 2
  • Please paste your code directly into the text of your question as opposed to linking an image. – Tom Aranda Nov 06 '17 at 16:31
  • Does the console output any error ? – Boulboulouboule Nov 06 '17 at 16:34
  • Thanks for response! I didn't get any console errors. nginx server directly throwing 404 error.@Boulboulouboule. But its working fine in my local machine. – Srinivas Nov 06 '17 at 16:58
  • Possible duplicate of [How to work around the 404 error on nginx?](https://stackoverflow.com/questions/47077428/how-to-work-around-the-404-error-on-nginx) – Shawn C. Nov 06 '17 at 18:39

3 Answers3

0

You will need to configure your web server to redirect all unknown (404) requests to index.html, which will allow the Angular application to then route your user to the desired route/view.

Krishna
  • 760
  • 3
  • 14
  • 30
  • Thanks for the response! can you please provide me sample code or any other links to help me for that @Krish – Srinivas Nov 06 '17 at 17:08
  • I know how to set up tomcat server but at this point i was not sure how to set up for ngINx. – Krishna Nov 06 '17 at 18:04
0

You can try something like this :

error_page 404 =200 /index.html;

Source : How do I force redirect all 404's (or every page, whether invalid or not) to the homepage?

Boulboulouboule
  • 4,087
  • 1
  • 13
  • 29
0
try_files $uri $uri/ /index.html;

This line has to be added in the nginx.conf incase of Windows server or in /etc/nginx/sites-enabled/default in case of ubuntu server.

That piece of code will look for angular paths before throwing a 404 error.

angular deployment check this out

Arokia Lijas
  • 441
  • 4
  • 14