I just deployed a NodeJS app on fly and connected a custom domain. I even generated a certificate. But the thing is when I go to the HTTPS version of the website, I get the secure site but if I go to the HTTP version, it takes to the non-secure site and I want the redirect to happen automatically. What should I do?
Not sure if you use express, but if you do:
const app = express();
...
app.use(function (req, res, next) {
if (
req.get('X-Forwarded-Proto') === 'http' &&
!['localhost', '127.0.0.1'].includes(
req.get('X-Forwarded-Host')?.split(':')[0] ?? ''
)
) {
res.redirect('https://' + req.headers.host + req.url);
} else {
next();
}
});
Redirects all http traffic to https, except for localhost and 127.0.0.1.
We’ve just shipped a new feature that handles this for you. It’s a simple configuration option in fly.toml
and you should be good to go.