Nginx:如何让重写规则忽略文件或文件夹

Nginx:如何让重写规则忽略文件或文件夹

问题描述:

我使用Nginx来提供SPA(单页应用程序),为了支持HTML5 History API,我必须将所有更深的路由重写回 /index.html ,所以我按照这篇文章它有效!这就是我现在放入nginx.conf的内容:

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;
}

但是有一个问题,我有一个 / assets 根目录下的目录包含所有的css,js,images,fonts东西,我不想重写这些url,我只是想忽略这些资产,我想怎么办?

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?

重写放入一个位置并使用其他位置 s作为资产/动态网址/等。

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.
    }
}