Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: how to let rewrite rules ignore files or folders

Tags:

html

nginx

I use Nginx to serve a SPA (Single Page Application), in order to support HTML5 History API I have to rewrite all deeper routes back to the /index.html, so I follow this article and it works! This is what I put in nginx.conf now:

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

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

However there's one problem, I have an /assets directory under the root contains all the css, js, images, fonts stuffs, I don't want to rewrite these urls, I just want to ignore these assets, how am I suppose to do?

like image 524
nightire Avatar asked Sep 29 '14 05:09

nightire


1 Answers

Put rewrite into one location and use other locations for assests/dynamic urls/etc.

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

    location / {
        rewrite ^ /index.html break;
    }

    location /assets/ {
        # Do nothing. nginx will serve files as usual.
    }
}
like image 70
Alexey Ten Avatar answered Sep 30 '22 20:09

Alexey Ten