Nginx:如何让重写规则忽略文件或文件夹 [英] Nginx: how to let rewrite rules ignore files or folders

查看:124
本文介绍了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.
    }
}

这篇关于Nginx:如何让重写规则忽略文件或文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆