Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: send all requests to a single html page

Using nginx, I want to preserve the url, but actually load the same page no matter what. I will use the url with History.getState() to route the requests in my javascript app. It seems like it should be a simple thing to do?

location / {
    rewrite (.*) base.html break;
}

works, but redirects the url? I still need the url, I just want to always use the same page.

like image 519
prismofeverything Avatar asked Sep 27 '22 02:09

prismofeverything


2 Answers

I think this will do it for you:

location / {
    try_files /base.html =404;
}
like image 217
Alex Howansky Avatar answered Oct 07 '22 05:10

Alex Howansky


Using just try_files didn't work for me - it caused a rewrite or internal redirection cycle error in my logs.

The Nginx docs had some additional details:

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

So I ended up using the following:

root /var/www/mysite;

location / {
    try_files $uri /base.html;
}

location = /base.html {
    expires 30s;
}
like image 36
Kevan Stannard Avatar answered Oct 07 '22 04:10

Kevan Stannard