Reputation:
I'm building a MEAN stack application which is behind nginx
The angular side of my application is all generic and will react differently depending on what URL the user goes to i.e if the user goes to my.app/cars it will go to a different web page than if the user goes to my.app/bicycles
Before the angular side of my application was generic, my nginx development file had all URLs hard coded
server {
listen 80;
location /cars {
...
}
....
}
However, now that the angular porition is generic, I want my nginx to set all requests to angular by default.
I'be been looking around online, and it seems that having
location / {
....
}
should achieve this, however, when I do this, I get a "this site can't be reached" error.
Any advice on how to achieve what I'm doing wrong to set nginx to angular by default?
I assume people do this if they have a "page not found" error however again I can't find anything.
Thanks
Upvotes: 1
Views: 391
Reputation: 4947
simple:
location / {
root /your/app/directory;
index index.html;
try_files $uri $uri/ /index.html;
}
you just need to tell nginx what to do if it cannot find a file.
try_files
will solve this issue for you.
if you request example.com/foo/bar/123
then it will try the following:
$uri
> /your/app/directory/foo/bar/123
$uri/
> /your/app/directory/foo/bar/123/index.html
(this is from the index
directive)/index.html
> /your/app/directory/index.html
PS:
if you specify try_files $uri $uri/ /;
then the last rule will take the index directive into account. similar to the second directive.
Upvotes: 2