【问题标题】:redirect all routes to https in nuxt project hosted in heroku将所有路由重定向到 Heroku 中托管的 nuxt 项目中的 https
【发布时间】:2019-10-30 22:53:10
【问题描述】:

我正在尝试创建一个中间件来将我的所有路由重定向到 https,我想我需要一个中间件,所以我在 Nuxt 的中间件文件夹中创建了一个 redirect.js 文件,然后我将其添加到 @ 987654322@:

router: {
  middleware: ["redirect"]
},

这是我的redirect.js 文件,它给我一个服务器错误:

export default function({ app }) {
  if (process.env.NODE_ENV === "production") {
   if (app.context.req.header("x-forwarded-proto") !== "https") {
  app.context.res.redirect(`https://${app.context.req.header("host")}${app.context.req.url}`);
   }
  }
}

【问题讨论】:

    标签: javascript http redirect heroku nuxt.js


    【解决方案1】:

    我发现了一种更简单的方法,我添加了包 redirect-ssl

    npm i redirect-ssl
    

    然后我在我的 nuxt.config.js 中添加了这一行:

    serverMiddleware: ["redirect-ssl"],
    

    【讨论】:

    • 如果您只想为生产启用它,请尝试此行:import redirectSSL from 'redirect-ssl'; export default { serverMiddleware: [redirectSSL.create({enabled: process.env.NODE_ENV === 'production'})]}
    • @AlbertiBuonarroti 我从github.com/nuxt-contrib/redirect-ssl#using-with-nuxtjs 复制了这个,但它不会重定向到我的 Heroku 部署。我在 heroku config vars 中将 NODE_ENV 设置为“生产”。
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 2015-06-16
    • 2019-08-31
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多