nginx conf for SPA

vue项目发布后如果是router是history的话,除了首页外的页面手动刷新的话,会出现404页面,就是找不到资源,hash模式不会出现这种问题,解决办法是try_files $uri /index.html 这样路由每次请求都指向index.html页面

在谷歌搜索:
nginx conf for SPA

修改 nginx.conf

root /var/www/your_site;

location / {
    try_files $uri /index.html;//这句话的作用
}
server {
  listen 80 default_server;
  listen [::]:80 default_server;

  root /your/root/path;

  index index.html;

  server_name you.server.com;

  location / {
    try_files $uri $uri/ @rewrites;
  }

  location @rewrites {
    rewrite ^(.+)$ /index.html last;
  }

  location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }

}

nginx + vue-router配置history模式访问
default.conf

nginx 代理解决跨域问题

        location / {
            root   C:/web/yy;
            index  index.html index.htm;
        }

        location /api/ {
            proxy_pass http://localhost:8080;
        }

有四种不同的路径匹配方式,
nginx 之 proxy_pass详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,256评论 19 139
  • 配置运行Nginx服务器用户(组) 用于配置运行Nginx服务器用户(组)的指令是user,其语法格式为: use...
    吃瓜的东阅读 4,582评论 0 41
  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 23,287评论 8 124
  • 刚看了简书里一篇关于讨论男孩为什么追女孩半年就不追了的问题。有的说对、有的说错。笔者也倾向于放弃的做法是对的。因为...
    PP07阅读 746评论 0 0
  • TFT策略 【自我介绍】 【学习目标】通过本次的学习,让大家在工作中能与客户快速建立合作关系,达到共赢 【FAB】...
    J杰伦阅读 356评论 0 0