【问题标题】:How do I config nginx to serve any path to a single specific html file?如何配置 nginx 以提供指向单个特定 html 文件的任何路径?
【发布时间】:2020-06-03 16:27:51
【问题描述】:

我使用 Vue 并预渲染了 index.html,以及 Vue 入口文件 app.html。 我需要将nginx 配置为按以下规则服务:

www.mydomain.com/ => go to /data/www/index.html
www.mydomain.com  => go to /data/www/index.html
www.mydomain.com/anything => anything else go to /data/www/app.html

这是我在ngnix.conf 中写的,但它不起作用:

server_name www.mydomain.com;
root /data/www/mydomain/;
location / {
            try_files $uri $uri/ $uri/index.html $uri/app.html /app.html ;
}

在任何情况下它都不服务app.html。 我如何配置来实现这一点? 谢谢。

【问题讨论】:

    标签: vue.js nginx prerender


    【解决方案1】:

    你可以试试这个吗?

    location = / {
        rewrite . /index.html break;
    }
    location / {
        rewrite . /app.html break;
    }
    

    【讨论】:

    • 我发现另一个location 是我在答案中的配置不起作用的原因:location ~ .*.(js|css)$?{expires 30d;},我删除了? 字符,它正在工作,不只是这个位置js和css文件过期有效吗?但毕竟,您的配置也运行良好!
    • js 和 css 文件的正确正则表达式必须是 .*\.(js|css)$(注意最后一个点之前的斜线)。
    • 甚至更简单:location ~ \.(js|css)$,但是在这个位置,每次请求您的服务器上不存在的 css 文件的 js 时,您都会收到 404 错误,并且这些请求不会传递给您的app.html。顺便说一句,我的配置会比你的有更好的性能,你可以阅读this article了解更多细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2016-03-03
    • 2018-12-10
    相关资源
    最近更新 更多