nginx - 如何将 vue.js 与 Nginx 一起使用?

标签 nginx vue.js

我想使用 Nginx 作为我的网络服务器和我自己的 Dropwiward REST API,使用 Vue.js 构建一个单页应用程序。此外,我使用 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/rescource 从后端获取信息。

我使用 HTML 和 javascript(vue.js) 构建了前端,到目前为止它已经运行良好。但是,当我想构建单页应用程序时,大多数教程都会提到 node.js。我怎样才能改用 Nginx?

最佳答案

将以下代码添加到您的 Nginx 配置中,如 VueRouter 文档中所述,here :

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

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

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

关于nginx - 如何将 vue.js 与 Nginx 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655869/

相关文章:

javascript - VueJS Jquery 数据表集成 : Attach method to dynamic html element

python - Nginx + Gunicorn + Flask : How to figure out the real base URL

css - 样式表未解析,因为在严格模式下不允许使用非 CSS MIME 类型

nginx - 如何修复此 Nginx 配置以正确代理 WebSocket 请求而不是返回 301?

javascript - Vue.JS 表格行 CSS 随数据变化

vue.js - 验证控制台中不存在的字段错误

nginx - 使用 DigitalOcean 在 Kubernetes 集群上为我的 Nginx-Ingress 生成通配符证书

docker - 如何将我的域名连接到运行 Ghost 博客的 Docker 容器

express - 如何使具有路由功能的 Vue.js 应用程序在 Heroku 中工作?

javascript - 如何在不创建 DOM 条目的情况下使用 v-for?