Rewriting nginx for pushState-URL's Rewriting nginx for pushState-URL's nginx nginx

Rewriting nginx for pushState-URL's


I ended up going with this solution:

server {    listen 80;    server_name example.com;    root /var/www/example.com;    # Any route containing a file extension (e.g. /devicesfile.js)    location ~ ^.+\..+$ {        try_files $uri =404;    }    # Any route that doesn't have a file extension (e.g. /devices)    location / {        try_files $uri /index.html;    }}

This way, at least I still get proper 404 errors if a file isn't found.


Here is what i did to my application. Every route ending with a '/' (except the root it self) will serve index.html :

  location ~ ^/.+/$ {    rewrite .* /index.html last;  }

You can also prefix your route :

Backbone.history.start({pushState: true, root: "/prefix/"})

and then :

  location ~ ^/prefix/ {    rewrite .* /index.html last;  }

Or define a rule for each case.


I managed it like this:

#set root and indexroot /var/www/conferences/video/;index  index.html;#route all requests that don't serve a file through index.htmllocation / {   if (!-e $request_filename){      rewrite ^(.*)$ /index.html break;   }}