0

I'm using nginx with Angular and want to return a 404 when a user go to page /error404 I did the redirection with angular routing module with this :

{ path: 'error404', component: PageNotFoundComponent},
{ path: '**', redirectTo: 'error404', pathMatch: 'full'}

and to return the 404 with nginx I try this in the conf file :

error_page 404 /error404;
location  /error404 {
 internal;
}

or just :

error_page 404 =301 /error404;

When I go directly to /error404 I get a 404 not found and not my page but when i use the angular redirection with random url i go well on the /error404 and print my page

2 Answers 2

2

Try to add this information below:

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

        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Angular router does not redirect to an url but to a given component.

You should create a component to handle 404 pages, it'll also save webserver load by not requesting it.

see this for further information : https://angular.io/guide/router#wildcard-route

10 Comments

But it's not what i did with this : my component to handle 404 { path: 'error404', component: PageNotFoundComponent} and this to redirect { path: '**', redirectTo: 'error404', pathMatch: 'full'} ?
I tries to redirect with the hashUrl so you will end up with a route like : http://your.server/#/error404
Here is the problem you are facing :) stackoverflow.com/q/40150393/7376078
Sorry I don't see how it can help me :( My problem is when I go directly to /error404 i have a 404 not found nginx but when i go to a random url of my website the redirection to /erro404 work well and print my page
Can you log me the two different Urls please. The one driven by angular and the other driven by nginx.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.