我正在尝试在单页应用程序上工作 - 我需要将所有 url 重写为 index.html,但允许现有静态文件(.css 和 .js)像通常在浏览器中一样提供。
这是我试图用来重写的代码,但它也为我的静态文件提供重写服务
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
我正在尝试在单页应用程序上工作 - 我需要将所有 url 重写为 index.html,但允许现有静态文件(.css 和 .js)像通常在浏览器中一样提供。
这是我试图用来重写的代码,但它也为我的静态文件提供重写服务
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
这应该有效:
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;
}
}
你实际上不需要在 nginx 中重写,只需像这样使用 try_files :
location / {
try_files $uri /index.html;
}
这适用于所有网址:
见http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files