【问题标题】:Nginx and Vuejs 3 configuration with baseurl and public path带有 baseurl 和公共路径的 Nginx 和 Vuejs 3 配置
【发布时间】:2021-04-07 21:57:36
【问题描述】:

我有一个带有历史模式的 VueJS 应用程序。我的网站是 example.com,我的应用程序的基本 URL 是 my-app,当您使用 example.com/my-app 进入我的应用程序时,它可以完美运行,您可以浏览页面。不幸的是,当您尝试直接访问具有特定路由的页面时,我遇到了 404。

这是我的 nginx 配置文件:

...
server_name example.com;
root /var/www/apps;
index index.html
try_files $uri $uri/ /index.html;
...

我在 vueJS 文档中找到了最后一行。

这是我的路由器配置:

const router = createRouter({
  base: process.env.VUE_APP_PUBLICPATH,
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

还有.env.production 文件:

NODE_ENV=production
BASE_URL=/my-app/
VUE_APP_PUBLICPATH=/my-app/

我以为我正确地遵循了文档,但事实并非如此,因为当我检查 Nginx 日志时,出现以下错误:

2020/12/31 17:05:21 [error] 21351#0: *16 open() "/var/www/apps/index.html" failed (2: No such file or directory), client: XXX.XXX.XXX.XXX, server: example.com, request: "GET /my-app/about HTTP/1.1", host: "example.com"

谁能帮帮我?在此先感谢:)

【问题讨论】:

标签: vue.js nginx vuejs3 html5-history


【解决方案1】:

我找到了解决方案,正在为网站编辑我的 nginx conf 文件:

...
server_name example.com
...

location /my-app/ {
  try_files $uri $uri/ /my-app/index.html;
}
...

就这么简单!

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 2019-11-11
    • 2021-11-09
    • 2015-11-14
    • 2012-06-29
    • 2017-10-10
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多