Azure上的节点服务器HTTPS和HTTP

时间:2013-11-20 10:21:01

标签: node.js azure https

我正试图在Azure上使用我的nodejs应用程序进行部署。

我启动了两台服务器:

  // public server
  publicServer.listen(publicServerPort, function() {
    console.log('Public server started and listening on port ' + publicServerPort);
  });

  // API server
  var credentials = {
    key : fs.readFileSync(key, 'utf8'),
    cert: fs.readFileSync(cert, 'utf8')
  };

  https
    .createServer(credentials, serverAPI)
    .listen(serverAPIPort, function(){
      console.log('API server started and listening on port ' + serverAPIPort);
    });

我读到Azure正在处理自己重定向到HTTPS,而且我也无法拥有两台服务器,因为只有一个端口是打开的。

如果我同时使用process.env.PORT,我得到(逻辑上):

Unaught exception: Error: listen EADDRINUSE

如果我用于公开process.env.PORT和HTTPS 443

Unaught exception: Error: listen EACCES

我可以有两台服务器吗? 如果没有如何正确处理路由(http和https不同)? 通过协议检测,例如?

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

我运行具有HTTP和HTTPS端点的Azure云服务(使用Node.js)。诀窍是让IIS为您处理SSL终止,以便您的Node.js应用程序仅侦听HTTP(使用process.env.PORT)。

我已将证书上传到Windows Azure门户,然后我的ServiceDefinition.csdef文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="my-cloud-service-name" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="my-cloud-service-name" vmsize="Small">
    <Imports />
    <Startup>
      <Task commandLine="setup_web.cmd &gt; log.txt" executionContext="elevated">
        <Environment>
          <Variable name="EMULATED">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
          </Variable>
          <Variable name="RUNTIMEID" value="node;iisnode" />
          <Variable name="RUNTIMEURL" value="http://az413943.vo.msecnd.net/node/0.8.4.exe;http://az413943.vo.msecnd.net/iisnode/0.1.21.exe" />
        </Environment>
      </Task>
      <Task commandLine="node.cmd ..\startup.js" executionContext="elevated" />
    </Startup>
    <Endpoints>
      <InputEndpoint name="Web" protocol="http" port="80" />
      <InputEndpoint name="WebSSL" protocol="https" port="443" certificate="my-certificate-name" />
    </Endpoints>
    <Certificates>
      <Certificate name="my-certificate-name" storeLocation="LocalMachine" storeName="CA" />
    </Certificates>
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Web" endpointName="Web" />
          <Binding name="WebSSL" endpointName="WebSSL" />
        </Bindings>
      </Site>
    </Sites>
  </WebRole>
</ServiceDefinition>

然后在我的Web.cloud.config文件的iisnode元素中,我确保提升HTTPS服务器变量:

<iisnode debuggingEnabled="false" devErrorsEnabled="false" loggingEnabled="false" node_env="production" promoteServerVars="HTTPS" />

这允许我使用一些Express.js中间件设置x-forwarded-proto头,这是识别HTTP请求的原始协议的事实标准:http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

exports.xForwardedProto = function(req, res, next) {
    if(!req.headers['x-forwarded-proto']) {
        if(req.headers['x-arr-ssl'] || req.headers['x-iisnode-https'] === 'on') {
            req.headers['x-forwarded-proto'] = 'https';
        }
    }

    next();
};

然后,当我想将HTTP请求重定向到HTTPS时,我使用这个Express.js中间件:

exports.httpsOnly = function(req, res, next) {
    if(req.protocol === 'http' && process.env.NODE_ENV && process.env.NODE_ENV != 'development') {
        return res.redirect(301, 'https://' + req.get('host') + req.url);
    }

    next();
};
相关问题