2014-05-01 4 views
21

मैं एक एक्सप्रेस ऐप लिख रहा हूं जो एक nginx सर्वर के पीछे बैठता है। मैं एक्सप्रेस के दस्तावेज़ीकरण के माध्यम से पढ़ रहा था और इसमें 'ट्रस्ट प्रॉक्सी' सेटिंग का उल्लेख किया गया था। सभी यह कहतेवास्तव में express.js में "ट्रस्ट प्रॉक्सी" क्या करता है, और क्या मुझे इसका उपयोग करने की आवश्यकता है?

विश्वास प्रॉक्सी रिवर्स प्रॉक्सी समर्थन, डिफ़ॉल्ट

मैं छोटा लेख यहाँ है कि nginx साथ नोड में सुरक्षित सत्र बताते पढ़ा रूप से अक्षम को सक्षम करता है।

http://blog.nikmartin.com/2013/07/secure-sessions-in-nodejs-with-nginx.html

तो मैं उत्सुक हूँ। क्या HTTPS का उपयोग करते समय केवल 'ट्रस्ट प्रॉक्सी' को सही मायने रखता है? वर्तमान में मेरा ऐप क्लाइंट और nginx के बीच बस HTTP है। यदि मैंने इसे अभी सत्य पर सेट किया है, तो क्या कोई दुष्प्रभाव/प्रतिक्रियाएं हैं जिनके बारे में मुझे अवगत होना चाहिए? क्या इसे अभी सही करने के लिए कोई मुद्दा है?

उत्तर

24

यह express behind the proxies guide

में विस्तार से समझाया गया है "विश्वास प्रॉक्सी" app.enable ('विश्वास प्रॉक्सी') के माध्यम से की स्थापना सक्षम करके, एक्सप्रेस ज्ञान होगा कि यह एक प्रॉक्सी के पीछे और उस बैठी है एक्स-फॉरवर्ड- * हेडर फ़ील्ड भरोसा किया जा सकता है, जो अन्यथा आसानी से खराब हो सकता है।

इस सेटिंग को सक्षम करने से कई सूक्ष्म प्रभाव पड़ते हैं। इनमें से पहला यह है कि X-Forwarded-Proto को रिवर्स प्रॉक्सी द्वारा सेट किया जा सकता है ताकि ऐप को यह बताने के लिए कि यह https या बस http है। यह मान req.protocol द्वारा प्रतिबिंबित होता है।

दूसरा बदलाव यह बनाता है कि req.ip और req.ips मान X-Forwarded-For की पतों की सूची के साथ पॉप्युलेट किए जाएंगे।

3

एनोटेट कोड विश्वास प्रॉक्सी के उपयोग की व्याख्या करने के लिए

var express = require('express'); 

    var app = express(); 

    // Set the ip-address of your trusted reverse proxy server such as 
    // haproxy or Apache mod proxy or nginx configured as proxy or others. 
    // The proxy server should insert the ip address of the remote client 
    // through request header 'X-Forwarded-For' as 
    // 'X-Forwarded-For: some.client.ip.address' 
    // Insertion of the forward header is an option on most proxy software 
    app.set('trust proxy', '127.0.0.1'); 


    app.get('/test', function(req, res){ 
     var ip = req.ip; // trust proxy sets ip to the remote client (not to the ip of the last reverse proxy server) 
     if (ip.substr(0,7) == '::ffff:') { // fix for if you have both ipv4 and ipv6 
     ip = ip.substr(7); 
     } 
     // req.ip and req.protocol are now set to ip and protocol of the client, not the ip and protocol of the reverse proxy server 
     // req.headers['x-forwarded-for'] is not changed 
     // req.headers['x-forwarded-for'] contains more than 1 forwarder when 
     // there are more forwarders between the client and nodejs. 
     // Forwarders can also be spoofed by the client, but 
     // app.set('trust proxy') selects the correct client ip from the list 
     // if the nodejs server is called directly, bypassing the trusted proxies, 
     // then 'trust proxy' ignores x-forwarded-for headers and 
     // sets req.ip to the remote client ip address 

     res.json({"ip": ip, "protocol": req.protocol, "headers": req.headers['x-forwarded-for']}); 
    }); 

// in this example the reverse proxy is expected to forward to port 3110 
var port = 3110; 
app.listen(port); 
// test through proxy: http://yourproxyserver/test, req.ip should be your client ip 
// test direct connection: http://yournodeserver:3110/test, req.ip should be your client ip even if you insert bogus x-forwarded-for request headers 
console.log('Listening at http://localhost:' + port); 
संबंधित मुद्दे