I have 1 domain name managed in Google Domains, that points to an AWS Lightsail instance, with a custom Node.js server.
For a while everything was working without issues. Then, I realized that the public IP of my AWS instance was dynamic, and I had the brilliant idea to change it to a static IP.
Then, I updated the IP on Google Domains to this new static IP. However, although the new IP is still pointing to the AWS instance, it is now doing so in an unexpected way for me.
I will try to explain the new behavior. First, let me share the Node.js server code that process the incoming requests on the AWS instance. This code, with the removal of unimportant parts, is as follows:
app.init = async function () {
var { servers } = app;
initHTTPSServer: {
let server = servers.https = m.https.createServer( {
key: await m.fileSystem.readFile( m.uri.certificates.sslKey ),
cert: await m.fileSystem.readFile( m.uri.certificates.ssl )
} );
server.addListener( 'request', servers.processRequest );
server.listen( 443 );
}
initHTTPServer: {
let server = servers.http = m.http.createServer( servers.processRequest );
server.listen( 80 );
}
}
app.servers.processRequest = async function ( request, response ) {
var { port } = request.socket.address();
console.log( port );
if( port == 80 ) return app.servers.redirectToHttps( request, response );
return app.servers.processHttpsRequest( request, response );
}
app.servers.redirectToHttps = function ( request, response ) {
var { hostname, pathname, search } = request.parsedURL,
port = `:443`; /* Also tried with empty string. I am not sure if before the IP update the port value was '' or `:443` */
console.log( `https://${ hostname }${ port }${ pathname }${ search }` );
response.writeHead(
302 /* Was 301 before the IP update, I changed to 302 while trying to debug */,
{ Location: `https://${ hostname }${ port }${ pathname }${ search }` }
);
response.end();
}
As shown in the code above, there are 2 servers, 1 HTTP and 1 HTTPS. The HTTP server is meant to only redirect requests to the HTTPS server.
The rest of the server logic is handled by the "processHttpsRequest" function, which, thus, is also meant to be executed for HTTP requests, after they are redirected to the HTTPS server.
This logic works fine on my localhost. Before the IP update, in Google Domains the domain name was set to forward to "h.ttp://my-old-dynamic-ip.com", and the logic was somehow also working fine.
In Google Domains, currently the forwarding config to the AWS instance is as in the image below:
forwards to "h.ttp:/./my-new-static-ip.com"
I didn't change the Redirect Type, the Path Forwarding, and the SSL configs, so I guess they are the same as before.
Currently, when Google Domains redirects a request to "h.ttp://my-new-static-ip.com", in the code above, console.log( 'port' ) is always 80, and as such "app.servers.processHttpsRequest()" never runs.
Even if I explicitly put the https protocol, as in "h.ttps://domain-name.com", Google Domains still redirects the request to "h.ttp://my-new-static-ip.com". Because of that, the "Location" address in the function "redirectToHttps", which is "h.ttps://domain-name.com/", always send the request back to the HTTP server, creating an infinite redirect loop.
If I change in Google Domains the forwarding address to "h.ttps://my-new-static-ip.com", I can access the site of "domain-name" when I do a request to "h.ttps://domain-name.com", but, instead of "h.ttps://domain-name.com", what appears in the navegation bar is "h.ttps://my-new-static-ip.com". Also, the SSL certificate is no longer valid, although it was valid before the IP update, and it only expires in December.
What I would like to achieve is the old behavior, in which "h.ttp://domain-name.com" redirects to "h.ttps://domain-name.com", and without changing "domain-name" to "my-new-static-ip", as well as without invalidating the SSL certificate. But I can't figure out why this problem happened after I changed the IP, and I am totally at loss on how to fix it.
Please, if somebody can shed some light on why this new behavior is happening, and how to fix things, I would be very happy.
On the Google Domains interface, I tried to look how to change the forwarding address such that "h.ttp://domain-name.com" redirects to "h.ttp://my-new-static-ip.com", and "h.ttps://domain-name.com" redirects to "h.ttps://my-new-static-ip.com". But I see no option to distinguish between HTTP and HTTPS requests coming from "domain-name"; it seems I am only able to config a single forwarding address for both protocols.
I tried consulting ChatGPT. It told me I have to do a reverse proxy. But I never did that before, and I don't know how to integrate it with the current code logic. Also, I am not even sure if ChatGPT understood correctly the problem and if the reverse proxy will fix it, as before the IP change the code was working fine without reverse proxies (and still works fine on localhost). So, before digging too deep into this rabbit hole, I would like to at least check with more knowledgeable people if this is indeed the way to fix things.