selvan
selvan

Reputation: 1243

How to use https connection in nodejs?

I am using Nginx+PHP setup to run PHP pages with 80 port. And also using nodejs with 8080 port. Now i want to connect SSL(https) connection for Nodejs.

How can be it is achievable? Pls.

Upvotes: 0

Views: 232

Answers (1)

Fabien Sacrepeigne
Fabien Sacrepeigne

Reputation: 156

You have two possibilities to achieve what you're trying to do :

1. Setting your SSL through Nginx

This way you will be able to use your SSL Certificate on your PHP sites aswell. To achieve that, you'll have to set some rules in your virtual hosts file in /etc/nginx/sites-enabled/foo. In the following example I've created a subdomain api.lvh.me (lvh.me redirecting to localhost)

server {
     listen 443 ssl;
     server_name api.lvh.me;

     ssl_certificate /path/to/certificate.crt;
     ssl_certificate_key /path/to/certificate.key;

     location / {
         proxy_pass http://127.0.0.1:8080/;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
     }
}

2. Setting HTTPS directly on your Nodejs server

I won't go into details, everything is written on Nodejs's documentation (HTTPS Nodejs Documentation)

Upvotes: 1

Related Questions