2014-05-01 40 views
21

Piszę aplikację ekspresową, która znajduje się za serwerem nginx. Czytałem dokumentację ekspresową i wspomniałem o ustawieniu "Trust proxy". Wszystko to mówi jestCo właściwie robi "proxy zaufania" w pliku express.js i czy muszę go używać?

zaufanie proxy Umożliwia odwrotnego proxy, domyślnie wyłączone

czytałem trochę artykuł, który wyjaśnia bezpiecznych sesji w węźle z nginx.

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

Więc jestem ciekaw. Czy ustawienie "proxy zaufania" na true ma znaczenie tylko przy korzystaniu z HTTPS? Obecnie moja aplikacja to po prostu HTTP między klientem a nginx. Jeśli teraz ustawię to na true, czy są jakieś skutki uboczne/reperkusje, których muszę się wystrzegać? Czy jest jakiś sens, aby teraz to ustawić?

Odpowiedz

24

to wyjaśnione szczegółowo w express behind the proxies guide

Poprzez umożliwienie „Trust” ustawienia proxy poprzez app.enable („proxy Trust”), Express posiada wiedzę, że siedzi za serwerem proxy, a Pola nagłówkowe X-Forwarded- * mogą być zaufane, w przeciwnym razie mogą być łatwo sfałszowane.

Włączenie tego ustawienia ma kilka subtelnych efektów. Pierwszym z nich jest to, że X-Forwarded-Proto może być ustawiony przez odwrotne proxy, aby powiedzieć aplikacji, że jest to https lub po prostu http. Ta wartość jest odzwierciedlana przez req.protocol.

Druga zmiana polega na tym, że wartości req.ip i req.ips zostaną wypełnione listą adresów X-Forwarded-For.

3

Adnotacje kod wyjaśnić wykorzystanie zaufania pełnomocnika

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); 
Powiązane problemy