Viral
Viral

Reputation: 461

Nuxt 6.14.6 redirect-ssl module not working into AWS

I am trying to configure redirect-ssl node module into nuxt application

Referece : https://www.npmjs.com/package/redirect-ssl

But when i load site in browser it gives me error with message -> Cannot GET /
ref. https://prnt.sc/xqsc05

Site works on SSL without redirect module. But I want to forcefully redirect all non HTTP request to HTTPS. I tried .htaccess code but I think nuxt do not supports it.

There is no error into terminal.

Tried following into nuxt.config.js different ways as following.

serverMiddleware: ["redirect-ssl"],

Into server/index.js file added following code

const redirectSSL = require('redirect-ssl')

async function start () {
.
.
app.use(redirectSSL)
}

enter image description here

How can we use .htaccess file into nuxt. I tried placing into root or nuxt project setup, but that did not worked for me.

Also anyone know how to implement CDN into nuxt other than build:publicPath variable.

Any help or suggestion for redirect-ssl module or nuxt with htaccess please ?

Upvotes: 2

Views: 987

Answers (1)

Viral
Viral

Reputation: 1327

Try out following way.

enter image description here

Into server/index.js

const redirectSSL = require('redirect-ssl');
const fs = require("fs");
const path = require("path");
const https = require('https');
const express = require('express');
const consola = require('consola');
const { Nuxt, Builder } = require('nuxt');
const app = express()
const pkey = fs.readFileSync(path.resolve(__dirname,  'domain_ssl.com.key'));
const pcert = fs.readFileSync(path.resolve(__dirname, 'domain_ssl.com.crt'));

const httpsOptions = {
   key: pkey,
   cert: pcert
};

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = false
async function start () {

  // Init Nuxt.js
  const nuxt = new Nuxt(config)
  const { host, port } = nuxt.options.server

  await nuxt.ready()

  // Build only in dev mode
  if (config.dev) {
        const builder = new Builder(nuxt)
        await builder.build()
  }

  // nuxt render and middleware
  app.use(nuxt.render)
  app.use(redirectSSL.create({ redirectPort: 443 }))
  
  // Listen the server
  app.listen(port, host)
  consola.ready({
        message: `Server listening on http://${host}:${port}`,
        badge: true
  })
  
  https.createServer(httpsOptions,app).listen(443, host)
  consola.ready({
        message: `Server listening on https://${host}:${port}`,
        badge: true
  })
}

start()

Above one is for forcefully SSL redirection. And for CDN use this steps. https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-build

Upvotes: 1

Related Questions