اذهب إلى المحتوى
  • 0

إعادة التوجيه إلى HTTPS في node.js

Tamim Fahed

السؤال

أحاول إضافة الشهادة وتعديل الاتصال إلى اتصال آمن https، وقمت باتباع الخطوات المذكورة في التوثيق الرسمي ل node.js:

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('test/test-key.pem'),
  cert: fs.readFileSync('test/test-cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hi");
}).listen(3000);

والآن عندما أقوم بتجربة الرابط من خلال curl مع وضع https:// تظهر لي العبارة " hi " أما عندما أقوم بوضع http يظهر لي الخطأ التالي:

curl: (52) Empty reply from server

كيف يمكنني إضافة وسيط معيّن لتحويل الاتصال من http إلى https بشكل تلقائي؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

يمكنك بشكل افتراضي تعريف الاتصال على أنه http بدلاً من https ثم تنفيذ إعادة التوجيه إلى https عند زيارة أي مسار ضمن التطبيق فتصبح بالشكل التالي:

var http = express();


http.get('*', function(req, res) {  

    res.redirect('https://' + req.headers.host + req.url);

})


http.listen(3000);

أو يمكنك بدلاً من الحصول على اسم النطاق والمسار بشكل ديناميكي ضمن الكود، كتابته بشكل يدوي كالتالي:

res.redirect('https://test.com' + req.url);

وعندها قد ترغب بإضافة الحماية اللازمة لمنع سرقة ملفات تعريف الارتباط أثناء اتصال http مع الخادم، فيمكنك إضافة التالي إلى الكود لديك:

const session = require('cookie-session');

app.use(
  session({
    secret: "secretSession",
    httpOnly: true,
    secure: true
  })
);

وعندها سيتم منع المتصفح من الوصول إلى ملفات تعريف الارتباط، وسيتم إرسالها فقط عند اتصال من نوع https لأننا وضعنا قيمة secure: true

كما يوجد طريقة أخرى مختصرة يمكنك تنفيذها من خلال استخدام الوسيط trust proxy بالشكل التالي:

app.enable('trust proxy')

app.use((req, res, next) => {
    req.secure ? next() : res.redirect('https://' + req.headers.host + req.url)
})

فيتم اختبار في حال كان الاتصال آمن سيتم تخديم المسارات وإلا سيتم التحويل إلى https ببساطة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

إذا كنت تستخدم nginx للتعامل مع SSL ، فخادم node سيستخدم http فقط.

    upstream nodejs { 
          server 127.0.0.1:4545 max_fails=0; 
    } 

   server { 
      listen 443; 
      ssl    on; 
      ssl_certificate    newlocalhost.crt; 
      ssl_certificate_key     newlocalhost.key; 
      server_name nodejs.newlocalhost.com; 

      add_header Strict-Transport-Security max-age=500; 

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

اما بإستخدام node فيجب عليك إنشاء خادم https:

// استيراد
const express = require('express');
var fs = require('fs');
const http = require('http');
const https = require('https');
const app = require('./path/to/your/express/app');

/* إنشاء خادم
 * https
*/
const httpsServer = https.createServer({
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.cert')
}, app);
httpsServer.listen(443, () => console.log(`HTTPS server listening: https://localhost`));

ومن ثم إعادة توجيه جميع طلبات خادم http الى خادم https

/* إعادة توجيه خادم
 * http
 * الى https
*/
const httpApp = express();
httpApp.all('*', (req, res) => res.redirect(300, 'https://localhost'));
const httpServer = http.createServer(httpApp);
httpServer.listen(80, () => console.log(`HTTP server listening: http://localhost`));

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...