Подтвердить что ты не робот

Что делает "доверенный прокси" на самом деле в express.js, и мне нужно его использовать?

Я пишу экспресс-приложение, которое находится за сервером nginx. Я читал экспресс-документацию, и он упомянул настройку "доверенного доверенного лица". Все, что он говорит,

trust proxy Включает поддержку обратного прокси, отключен по умолчанию

Я прочитал небольшую статью, в которой объясняется, что Secure Sessions в Node имеет nginx.

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

Так что мне любопытно. Использует ли параметр "доверенный прокси" значение true только при использовании HTTPS? В настоящее время мое приложение - это просто HTTP между клиентом и nginx. Если я установил его в настоящее время, есть ли какие-либо побочные эффекты/последствия, о которых я должен знать? Есть ли смысл установить его сейчас?

4b9b3361

Ответ 1

Это подробно объясняется в за поддержкой прокси-сервера

Включив настройку "доверенного прокси" с помощью app.enable( "доверенный прокси" ), Express будет знать, что он сидит за прокси-сервером и что поля заголовка X-Forwarded- * могут быть доверены, что в противном случае может быть легко подделать.

Включение этого параметра имеет несколько тонких эффектов. Первым из них является то, что X-Forwarded-Proto может быть установлен обратным прокси, чтобы сообщить приложению, что это https или просто http. Это значение отражается req.protocol.

Второе изменение это означает, что значения req.ip и req.ips будут заполнены списком адресов X-Forwarded-For.

Ответ 2

Аннотированный код для объяснения использования доверенного прокси

    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);