How to serve all routes on a production server exactly?

I’ve built an ember app recently, started on a local machine using ember-cli, everything works great until I deploy this app on a debian server.

On local machine, I can access an url like http://locahost:4200/posts/new directly, but on server this will return me a 404 error, but the root url works both fine. In addition, if I enter the root url first, then transition to /posts/new, it works as it should be.

I’ve also tried to find some instructions in ember guides, but unfortunately the only thing seems relevant is just one sentence:

Keep in mind that your server must serve the Ember app at all the routes defined here.

But how exactly? What should I do to make it right? Hope someone can give me some hints, simple examples would be better, thanks.

FYI, I serve this app by using nginx, and rails as api backend service.

2 Likes

I think I understand for a little now, since a url like http://domain.com/posts/new which the /posts/new part doesn’t exists for Nginx, so 404 does make sense. (Although I still don’t know why localhost don’t care, anyone knows?)

So maybe the simplest solution is to add a rewrite rule for Nginx, for now I am trying this one:

server {
    listen 80 default;
    server_name my.domain.com;
    root /path/to/app/root;

    rewrite ^(.+)$ /index.html last;
}

However this rule also rewrites all requests to files under /assets folder, I am searching for a solution but no luck so far. Does anyone knows how to solve this? or I’m just completely wrong?

1 Like

OK, I finally solve this issue by reading a great book: Nginx HTTP Server - Second Edition (Community Experience Distilled) , I should do more homework on Nginx, sometimes it is very useful.

Here’s solution:

server {
    listen 80 default;
    server_name my.domain.com;
    root /path/to/app/root;

    location / {
        rewrite ^ /index.html break;
    }
    
    location /assets/ {
        # do nothing and let nginx handle this as usual
    }
}
8 Likes

I’ve had to do something very similar for lighttpd (on Apache it’s no problem, it’ll scan the filesystem for what it can serve). Only seen your post just now or I would’ve replied. There are probably stack overflow questions about it already, but if not, you probably should create one plus the answer.

Yup, a catch-all route. Same principle for every deployment method you want to use.

Simpler:

server {
  listen 80;
  root /home/app/dist/;

  location / {
    try_files $uri /index.html;
  }
}
7 Likes

Thank you for providing solutions to today for a long time could not find a reason! Thanks again! ! :slight_smile: