Nginx history fallback配置
基本(能用就行)
# root /var/www/html;
location / {
# ...
try_files $uri /index.html;
}
更佳(仅在浏览器接受HTML时才尝试响应后备HTML)
# root /var/www/html;
location / {
# ...
set $fallback_uri /index.html;
if ($http_accept !~ text/html) {
set $fallback_uri $uri;
}
try_files $uri $fallback_uri =404;
}
兼容(与指令index
,autoindex
并存)
# root /var/www/html;
location / {
index index.html;
autoindex on;
# ...
set $fallback_uri /index.html;
if ($http_accept !~ text/html) {
set $fallback_uri /null;
}
try_files $uri $uri/ $fallback_uri =404;
}
WebpackDevServer history fallback配置
在webpack.config.js
基本
module.exports={
//...
devServer: {
//...
historyApiFallback: true,
},
};
更佳
module.exports={
//...
devServer: {
//...
historyApiFallback: {
htmlAcceptHeaders: ['text/html'], // 仅当浏览器接受HTML时,服务器才响应/index.html
},
},
};