【问题标题】:404 on refresh with Vue Router and Nginx使用 Vue Router 和 Nginx 刷新时出现 404
【发布时间】:2021-06-05 05:51:58
【问题描述】:

我在 Ubuntu 服务器上有一个项目,在刷新或手动输入 URL 时会产生 404 错误。例如,我可以访问 example.com,但无法访问 example.com/home 或在该页面上刷新而不会收到 404。单击链接按预期工作。我在历史模式下使用 Vue 路由器,并在 nginx.conf 文件的 http 部分添加了以下内容:

server {

            listen 80;
            server_name example.com;

            root /var/www/example.com;

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

我可以使用 sudo nginx -t 测试配置并通过,但我仍然收到 404 错误。值得一提的是,我在此服务器上有多个服务器块。

【问题讨论】:

  • 为什么不在location / 行之前添加index index.html
  • 是的,我最终发现我需要这样做。现在工作正常。

标签: nginx vue-router


【解决方案1】:

我对 nginx.conf 做了一些修改,这很有效:

server {

            listen 80;
            server_name example.com;

            index index.html;

            location / {
                    root /var/www/example.com;
                    index index.html;
                    try_files $uri $uri/ /index.html;
            }
    }

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 2021-03-08
    • 2019-10-06
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2017-12-11
    • 2019-01-14
    相关资源
    最近更新 更多