Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring HTTPS on Nginx and NodeJS

I'm using Nginx to publish static content on port 80 and a 433 redirect (with SSL) to the NodeJS. The configuration of Nginx is as follows:

server {
    listen 443 ssl;

    ssl_certificate /opt/projetos/nodejs-project-ssl/vectortowns-cert.pem;
    ssl_certificate_key /opt/projetos/nodejs-project-ssl/vectortowns-key.pem;
    ssl_protocols        SSLv3 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;

    server_name 127.0.0.1:443;

    location / {
            proxy_pass  https://127.0.0.1:8443;
            proxy_redirect off;
            proxy_set_header Host $host ;
            proxy_set_header X-Real-IP $remote_addr ;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
            proxy_set_header X-Forwarded-Proto https;
    }

}

With NodeJS, Express, and EJS, I'm publishing dynamic content to port 8443, also configured to use HTTPS. See the javascript code below (only the parts that are important for the question).

// other codes...
/* Enable https */
var privateKey = fs.readFileSync('/opt/projetos/nodejs-project-ssl/vectortowns-key.pem');
var certificate = fs.readFileSync('/opt/projetos/nodejs-project-ssl/vectortowns-cert.pem');
var credentials = {
    key: privateKey,
    cert: certificate
};
// other codes...
/* Controllers */
app.use(require('./controllers'));
https.createServer(credentials, app).listen(
    configuration.server.port,
    configuration.server.address,
    function(){
        logger.info('Server started: ' + configuration.server.address + ':' + configuration.server.port);
});

My questions are:

  1. Do I need to configure the SSL certificate and key in Nginx and NodeJS or just in one of them?
  2. If I only need one, what would be the best option (NodeJS or Nginx)?

Thank you!!

like image 432
lgapontes Avatar asked Jan 23 '17 22:01

lgapontes


People also ask

How do I enable HTTPS in nginx?

To set up an HTTPS server, in your nginx. conf file include the ssl parameter to the listen directive in the server block, then specify the locations of the server certificate and private key files: server { listen 443 ssl; server_name www.example.com; ssl_certificate www. example.com.

How can you create an https server with NodeJS?

To built an HTTPS server with nodeJs, we need an SSL (Secure Sockets Layer) certificate. We can create a self-signed SSL certificate on our local machine. Let's first create an SSL certificate on our machine first. After running this command, we would get some options to fill.

Can you use nginx with node js?

Fortunately, you can cache static content, reverse proxy and load balance among multiple application servers, and manage port contention between clients using Nginx. This makes Nginx an excellent tool for increasing Node. js performance.

How do I run node js in HTTPS?

To start your https server, run node app. js (here, app. js is name of the file) on the terminal. or in your browser, by going to https://localhost:8000 .


1 Answers

Use Nginx (and Nginx only) for SSL, that's the standard. As you set, Nginx works as a reverse proxy so it will feed you program with local unencrypted data for the given encrypted data on port 443, so it won't work if you also use SSL on your node program

like image 72
Cristóvão Trevisan Avatar answered Sep 29 '22 13:09

Cristóvão Trevisan