如何在Nginx中使用vue.js?

如何在Nginx中使用vue.js?,nginx,vue.js,Nginx,Vue.js,我想用Vue.js构建一个单页应用程序,使用Nginx作为我的web服务器和我自己的dropwiwardrestapi。此外,我使用Axios调用我的REST请求 我的nginx配置看起来像 server { listen 80; server_name localhost; location / { root path/to/vue.js/Project; index index.html index.ht

我想用Vue.js构建一个单页应用程序,使用Nginx作为我的web服务器和我自己的dropwiwardrestapi。此外,我使用Axios调用我的REST请求

我的nginx配置看起来像

server {
    listen       80;
    server_name  localhost;

    location / {
        root     path/to/vue.js/Project;
        index    index.html index.htm;
        include  /etc/nginx/mime.types;
    }
    location /api/ {
        rewrite ^/api^/ /$1 break;
        proxy_pass http://localhost:8080/;
    }
}
目前,我可以调用我的
localhost/api/path/to/ressource
从后端获取信息。


我用HTML和javascript(vue.js)构建了前端,到目前为止,这两种语言都很有效。然而,当我想要构建一个单页应用程序时,大多数教程都提到node.js。如何使用Nginx

将以下代码添加到Nginx配置中:

location / {
  try_files $uri $uri/ /index.html;
}
以下代码段取自vue路由器文档,它是

此外,您还需要在VueRouter上启用历史记录模式:

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

如果要使用Nginx创建单页应用程序,需要使用vuerouter。在服务器端(nginx)不需要很多配置。把你的根指向你的索引file@samayo不过,如果使用vue路由器,OP将需要一个全覆盖路由,这样访问
foo/bar/which
的用户就不会得到404。啊,很好的捕获。可能
尝试\u文件$uri$uri//$query\u字符串??我不确定Though谢谢你的提示,我想它也在Github gist上提到过。但是,这是一个很好的方法,还是有一种方法可以将vue cli网页包与nginx一起使用?这个答案主要针对vue cli网页包1,但如果您使用vue路由器,它可以与您的配置一起使用。如果您不使用vue路由器,请使用标准nginx html配置。再次感谢。我理解正确吗。我使用webpack和vue cli进行开发,然后可以使用
npm run dev
运行开发Web服务器,但稍后可以将其部署到nginx@A.Dumas好的,尝试
npm运行build
,然后有一个dist文件夹。将文件夹的内容复制到服务器。