当你使用 history 模式时,url就像正常的url,例如 “http://yoursite.com/mapp/subpage”,不过这种模式还需要后台配置支持。因为我们的应用是单页面应用,如果后台没有正确的配置,当用户在浏览器直接访问 "http://yoursite.com/mapp/subpage"就会返回 404。对于后台如何配置支持,官网:https://router.vuejs.org/zh/guide/essentials/history-mode.html提供的Nginx配置方案:

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

子目录部署

若项目没有直接部署在Nginx主目录下,而是在其子目录,如:
Nginx配置root路径为:/usr/local/nginx/html,子目录路径为:/usr/local/nginx/html/your_folder,此时Nginx应该这样配置:

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

Nginx 转发时Header中信息丢失

通过Nginx转发后, Header中access_token信息丢失,经查,发现Nginx在转发时,header中带下划线_的属性默认不转发,需要增加配置

underscores_in_headers on;
server {
        listen       80;
        server_name  127.0.0.1;
        
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        underscores_in_headers on;
		...(其他略)
}

官网: https://router.vuejs.org/zh/guide/essentials/history-mode.html

Logo

前往低代码交流专区

更多推荐