httpnginxsslnext.jshttps

How to I deploy nextjs application in a production environment, using ssl?


There does not seem to be a definitive answer to this. I have seen other questions posted here where people suggest using nginx to proxy ssl and run the next app as http. I've also seen posts about --experimental-https.

My use case: I have nginx setup with ssl. However, I require next to be launched with https for the crypto module to work.

I tried adding --experimental-https to my package.json like such:

  "scripts": {
    "dev": "next dev --turbopack --experimental-https",
    "build": "next build",
    "start": "next start --experimental-https",
    "lint": "next lint",
    "start:dev": "cross-env-shell ./node_modules/.bin/next dev"
  },

Running npm run dev works a charm, enabling ssl support.

Doing a build and running npm run start gives the error:

error: unknown option '--experimental-https'

So how would I actually launch next with https support in a production environment?


Solution

  • So some research has suggested that it is not possible to launch a NextJS app using npm run start with https enabled. It seems it is only possible do do this using --experimental-https in dev mode.

    Instead, you have to use a service that supports https like nginx and proxy the results. I had a suspicion of this, but I wanted clarification which I now have.

    Just a side note - none of the answers I found (though yes, a solution to the problem) did not explicitly state whether it was possible to do so, which is what my question was was asking. I was not asking how to setup nginx for https with nextjs in production.