Configure HTTPS for Express and Nginx

I am trying to configure an ExpressJS application to connect https. The express server runs on localhost: 8080 and secure localhost: 8443.

Here is the server.js code associated with https:

var app = express();

var https = require('https');

const options = {
    cert: fs.readFileSync('/etc/letsencrypt/live/fire.mydomain.me/fullchain.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/fire.mydomain.me/privkey.pem')
};

app.listen(8080, console.log("Server running"));
https.createServer(options, app).listen(8443, console.log("Secure server running on port 8443"));

And here is my Nginx configuration:

server {
    listen 80;
    listen [::]:80;
    server_name fire.mydomain.me;

    location / {
        proxy_pass http://localhost: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;
    }
}

server {
    listen 443;
    listen [::]:443;
    server_name fire.mydomain.me;
    location / {
        proxy_pass https://localhost:8443;
        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;
    }
}

What I've done:

  • Create an SSL certificate using the Letsencrypt tool certonly for the fire.mydomain.me domain.
  • Configuring nginx.
  • Configuring the server.js node application.
  • Adding TCP rules for port 443 in Ufw.

I tried

+4
2

HTTPS nginx Node, . HTTP- 80 HTTPS- 443 Node 8080 - TLS.

server.js :

var app = express();

app.listen(8080, console.log("Server running"));

nginx proxy_pass http://localhost:8080; HTTP 80 HTTPS 443.

. loopback , , , , , . , https://nodejs.org/en/blog/vulnerability/ OpenSSL, , SSL Node loopback. . Node GitHub .

+11

@rsp, Nginx:

server {
listen 80;
listen 443 ssl;

server_name fire.mydomain.me;

ssl_certificate     /etc/letsencrypt/live/fire.mydomain.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/fire.mydomain.me/privkey.pem;

location / {
    proxy_pass http://localhost: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;
   }
}
+5

Source: https://habr.com/ru/post/1672152/


All Articles