Nginx将静态文件重写为index.html

时间:2012-11-09 21:33:53

标签: nginx

我正在尝试使用单页应用程序 - 我需要将所有URL重写为index.html,但允许现有的静态文件(.css和.js)按照通常在浏览器中的方式进行处理。 / p>

这是我试图用来重写的代码,但是它也可以重写我的静态文件

if (!-e $request_filename)
{
    rewrite ^/(.*)$ /?/$1 last;
    break;
}

2 个答案:

答案 0 :(得分:2)

这应该有效:

server {
    listen          1.2.3.4:80;
    server_name     domain.eu;
    root            /usr/local/www/domain.eu/public;

    try_files       $uri @rewrites;

    location @rewrites {
        rewrite   ^/favicon.ico$   /pictures/favicon.ico   last;
        rewrite   ^                /index.html             last;
    }
}

答案 1 :(得分:2)

你实际上并不需要在nginx中重写它,只需像这样使用try_files:

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

这对所有网址的作用是什么:

  1. 尝试完全静态文件名匹配,如果存在则提供
  2. 如果1没有提供任何服务,则服务器/index.html改为
  3. 请参阅http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

相关问题