我的前端是uniapp,后端是fastadmin,前后端分离的网站项目,只使用一个域名www.域名.com,uniapp使用的history模式,运行基础路径设置为/,放在fastadmin框架的public目录下,运行顺序是index.html,当前网站的伪静态设置为:
location ~* (runtime|application)/{
return 403;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
},网站能正常的运行,但是有一个问题就是当刷新页面就会出现404。
我们既要满足正常调用fastadmin后端api,又能实现uniapp的history模式下防止刷新页面404的规则try_files $uri $uri/ /index.html,还必需前后端都使用一个域名,经过踩坑作个笔记,可以使用以下伪静态即可满上面的需求:
# 禁止访问敏感目录
location ~* (runtime|application)/ {
return 403;
}
# FastAdmin后端处理
location /api/ {
if (!-e $request_filename){
rewrite ^/api/(.*)$ /index.php?s=api/$1 last;
break;
}
}
# 处理uniapp的History模式
location / {
try_files $uri $uri/ /index.html;
}