目前web开发 使用一般前后端分离技术,并且前端负责路由。为了美观,会采用前端会采用h5 history 模式的路由。但刷新页面时,前端真的会按照假路由去后端寻找文件。此时,后端必须返回index(index.html)文件才不至于返回404。
nginx 部署一个单页应用很简单:
location / {
root html;
try_files $uri /index.html index.html;
}
但如果几个单页应用同时需要部署在同一台电脑上,并且都需要占用80或者443端口,就不太容易了。
介绍2种相同ip端口部署多个单页应用(前端路由)的方法。
server {
listen 80;
root /root/test; #web服务器目录;
location ^~ /a/{
try_files $uri /a/index.html; #如果找不到文件,就返回 /root/test/a/index.html
}
location ^~ /b/{
try_files $uri /b/index.html; #如果找不到文件,就返回 /root/test/b/index.html
}
}