美文网首页
nginx conf for SPA

nginx conf for SPA

作者: 别过经年 | 来源:发表于2017-05-11 15:56 被阅读1334次

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详解

相关文章

网友评论

      本文标题:nginx conf for SPA

      本文链接:https://www.haomeiwen.com/subject/gziotxtx.html