G
G
GNG9992021-09-26 14:59:58
Nginx
GNG999, 2021-09-26 14:59:58

How to set up ssl in node js?

I have a react spa application on ubuntu nginx - located in /var/www/bio-global.ru/admn
Config in sites-available

upstream remoteApplicationServer {
    server 10.10.10.10;
}

upstream remoteAPIServer {
    server 20.20.20.20;
    server 20.20.20.21;
    server 20.20.20.22;
    server 20.20.20.23;
}


server {
    server_name www.bio-global.ru bio-global.ru;
    root /var/www/html;
    index index.html;
        location /remote { //мое реакт спа приложение
            alias /var/www/bio-global.ru/admn;
            try_files $uri $uri/ =404;
        }
          location /service {  //бэкэнд приложение node js 
            proxy_set_header   Host             $host:$server_port;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:5000;
        }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/bio-global.ru/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/bio-global.ru/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

I made a ssl certificate using letsencrypt.
I have a node backend application - located in /var/www/bio-global.ru/service (there is a configuration above in the config)
it runs on port 5000 of
its index.js
require('dotenv').config()
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express')
const sequelize = require('./db')
const models = require('./models/models')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const router = require('./routes/index')
const errorHandler = require('./middleware/ErrorHandlingMiddleware')
const path = require('path')

const PORT = process.env.PORT || 5000
//const privateKey = fs.readFileSync('/etc/letsencrypt/live/bio-global.ru/privkey.pem', 'utf8');
//const certificate = fs.readFileSync('/etc/letsencrypt/live/bio-global.ru/cert.pem', 'utf8');
//const ca = fs.readFileSync('/etc/letsencrypt/live/bio-global.ru/chain.pem', 'utf8');

/*const credentials = {
        key: privateKey,
        cert: certificate,
        ca: ca
}*/
//const httpServer = http.createServer(app);
//const httpsServer = https.createServer(credentials, app);
const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static(path.resolve(__dirname, 'static')))
app.use(fileUpload({}))
app.use('/api', router)

// Обработка ошибок, последний Middleware
app.use(errorHandler)

const start = async () => {
    try {
        await sequelize.authenticate()
        await sequelize.sync()
        app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
    } catch (e) {
        console.log(e)
    }
}
start()

with React application, requests for the back application go to the url bio-global.ru:5000/api , but the error is blocked mixed content
, as I understand it, do I need to configure ssl in the node js application? in the index file to uncomment the lines ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2021-09-26
@ky0

What happens in the browser should not go unencrypted to strange ports. You also have a normally configured proxy in the config /service, do the same for /api.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question