Open In App

How to Configure Google Compute Engine to use HTTPS for Node.js Server ?

In this article, we will have a look at the configuration of HTTPS for the Node.js server deployed in Google Compute Engine. We will see how we can set up a Node.js server with reverse proxy with an NGINX server having HTTPS configuration. 

We will use GCE (Google Compute Engine) Virtual machine for setting up our application. NGINX is a free web server that we are going to use. For this server, we will set up HTTPS by installing an SSL certificate. We will forward HTTPS traffic from NGINX to the local HTTP Node.js server running at localhost using reverse proxy. So, let’s start setting up our server.



Prerequisites:

 



Steps to install NGINX, OpenSSL, NodeJS, and NPM on GCE virtual machine:

Firstly, update your packages using the below command. 

sudo apt-get update

Run the Below Command to install NodeJS and npm.

sudo apt-get install nodejs
sudo apt-get install npm

Install NGINX and OpenSSL using the below commands.

sudo apt-get install nginx
sudo apt-get install openssl

Steps to configure a new GCE VM:

 

 

Steps to configure the NodeJS project:

For this tutorial, you can use an existing NodeJS project OR follow the below steps to create a sample project.

npm init
npm install express




const express= require('express');
const app=express()
const port=3000
  
app.get('/',(req,res)=>{
    res.send('Hello From GFG Server');
});
  
app.listen(port,()=>{
    console.log(`Server started on port ${port}`);
})

node app.js

 

Steps to configure NGINX:

sudo apt-get install -y nginx

 

 

Setting up HTTPS on NGINX:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx.key -out /etc/ssl/certs/nginx.crt

 

sudo openssl dhparam -out /etc/nginx/dhparam.pem 4096

ssl_certificate /etc/ssl/certs/nginx.crt;
ssl_certificate_key /etc/ssl/private/nginx.key;

ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection “1; mode=block”;
ssl_dhparam /etc/nginx/dhparam.pem;
ssl_ecdh_curve secp384r1;

 

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    include snippets/self-signed.conf;

    server_name example.com www.example.com;
    ...
}

Setting Up Reverse proxy on NGINX: 

 

sudo service nginx restart

 

 


Article Tags :