Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular routes not directly accessible in nginx

Here's the deal: I set up the simplest nginx configuration for practicing angular and I configured the routeProvider to use html5mode. So far, so good! I set the base to '/', my links to /view1 and /view2 works fine, but only when I click these links in my index page. When I try to access them directly in the browser (or reload the page), nginx returns me a 403 forbidden. That's a simple configuration but I can't find a simple explanation about it :(

I want to set things up in a way that any route works fine without have to explicitly set it (one by one) in my nginx site.conf. By the way, permissions are good !

Here's the actual conf file:

server {
    listen  *:80;
    server_name rbs.ang;
    root    /_dev/angularjs/app;
    index   index.html;
    charset UTF-8;

    location / {
        expires -1;
        add_header  Pragma "no-cache";
        add_header  Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        try_files   $uri $uri/ index.html;
   }
}
like image 567
Ricardo Schalch Avatar asked Jun 12 '15 16:06

Ricardo Schalch


1 Answers

You need to configure a fallback route on your server to serve index.html file when a route is not matched. This works:

location / {
    try_files $uri $uri/ /index.html?$query_string;
}
like image 131
Rahul Kumar Avatar answered Nov 15 '22 09:11

Rahul Kumar