Redirect HTTP to HTTPS
Description
It would be nice if we were able to automatically redirect HTTP requests to Vite to HTTPS if it is meant to be running via HTTPS.
#13164 is a prior issue. @bluwy closed it with the following remarks:
A server can't listen to two protocols at once without creating two separate servers, and I don't think Vite should be doing that by default, especially there's a possibility someone intentionally has another http server for a reason.
Going with fi3ework's solution would be the easiest for now. You could also write a Vite plugin to spin up the second http server to redirect to the https server if you prefer.
HTTP and HTTPS (at least, up to HTTP2) are protocols on top of TCP, so if one spins up a TCP server and listens on a port, and then inspects an incoming connection to determine whether it wants to establish an HTTP or HTTPS connection, that can then be forwarded to the desired server.
I tried writing a plugin for this, but unfortunately the HTTP2 session that we have access to in configureServer isn't so easy to work with. At a plugin level, this is hard to do, it's much easier to set this up before the Vite dev server is started.
cc @richardtallent-erm
Suggested solution
For now, I've worked around this by creating script that starts a TCP server and forwards the connection to an Express app. The logic was lifted from https://github.com/mscdex/httpolyglot. It would be trivial to build this into Vite, and I'm happy to submit a PR if the maintainers indicate that they would accept such a feature.
server.js:
import express from 'express';
import http from 'node:http';
import http2 from 'node:http2';
import net from 'node:net';
import { fileURLToPath } from 'node:url';
import { createServer as createViteServer } from 'vite';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const viteServer = await createViteServer({
root: __dirname,
server: { middlewareMode: true },
});
const { config } = viteServer;
if (!config.server.https) {
http.createServer(viteServer.middlewares).listen(config.server.port);
} else {
const httpServer = http.createServer(
express().use((req, res) => {
res.redirect(
307,
`https://${req.hostname}:${config.server.port}${req.originalUrl}`
);
})
);
const httpsServer = http2.createSecureServer(
{
maxSessionMemory: 1000,
...config.server.https,
allowHTTP1: true
},
viteServer.middlewares
);
const onError = () => {};
net
.createServer(function onConnection(socket) {
socket.on('error', onError);
const data = socket.read(1);
if (data === null) {
socket.removeListener('error', onError);
socket.on('error', onError);
socket.once('readable', () => {
onConnection(socket);
});
} else {
socket.removeListener('error', onError);
const firstByte = data[0];
socket.unshift(data);
if (firstByte < 32 || firstByte >= 127) {
httpsServer.emit('connection', socket);
} else {
httpServer.emit('connection', socket);
}
}
})
.listen(config.server.port);
}Alternative
Well, the alternative would be to manually implement the script above, as I have done, but it seems rather reasonable to me that this would be an ergonomic default, or at least something one could opt in to with a feature flag.
Additional context
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Source: vitejs/vite