Question

Nginx 404 error with existing urls Angular 2 One page application with routing

I have a one-page angular 2 application that uses routing. When I hit my base URL example.com then the page loads correctly. However example.com/dashboard results in a 404 error.

My server logs have this:

2017/02/14 05:37:22 [warn] 680#680: conflicting server name "example.com" on 0.0.0.0:80, ignored
2017/02/14 05:37:22 [warn] 680#680: conflicting server name "www.example.com" on 0.0.0.0:80, ignored
2017/02/14 05:37:22 [warn] 680#680: conflicting server name "example.com" on [::]:80, ignored
2017/02/14 05:37:22 [warn] 680#680: conflicting server name "www.example.com" on [::]:80, ignored
2017/02/14 05:48:43 [error] 997#997: *1 open() "/var/www/example.com/html/dashboard" failed (2: No such file or directory), client: 41.180.12.42, server: example.com, request: "GET /dashboard HTTP/1.1", host: "example.com"
2017/02/14 05:49:03 [error] 997#997: *1 open() "/var/www/example.com/html/dashboard" failed (2: No such file or directory), client: 41.180.12.42, server: example.com, request: "GET /dashboard HTTP/1.1", host: "example.com"

The page dashboard.html does not exist because its I’m using angular’s routing and I built the application using webpack.

Here is my server block:

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

        root /var/www/example.com/html;

	index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
            try_files $uri $uri/ =404;
         }
}

Thank you for your help.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@marcduplessis1

Instead of using:

try_files $uri $uri/ =404;

… try using:

try_files $uri $uri/ /index.html;

This assumes that Angular is setup to handle requests that are sent to index.html and will route all requests accordingly.

@jtittle Thank you for the help! that worked :)

Can you tell me where to add this code. I am getting the same issue when trying to refresh the redirected page.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.