1

我正在尝试在单页应用程序上工作 - 我需要将所有 url 重写为 index.html,但允许现有静态文件(.css 和 .js)像通常在浏览器中一样提供。

这是我试图用来重写的代码,但它也为我的静态文件提供重写服务

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

2 回答 2

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;
    }
}
于 2012-11-09T22:58:14.357 回答
2

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

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

这适用于所有网址:

  1. 尝试精确的静态文件名匹配,如果存在则提供它
  2. 如果 1 没有提供任何服务,则改为 server /index.html

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

于 2012-11-10T00:45:35.477 回答