Azure上的节点服务器HTTPS和HTTP

3

我正在尝试在Azure上首次部署我的Node.js应用程序。

我启动了两个服务器:

  // 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 来为公共端口,使用 443 来进行 HTTPS:

Unaught exception: Error: listen EACCES

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

谢谢您的帮助。


如果您能提供Azure服务定义文件的至少一部分(如何配置“Runtime > Environment”和“Endpoints”),那将非常有益。此外,您使用Azure网站还是Azure云服务? - Tom
1个回答

4

我运行一个使用Node.js的Azure云服务,具有HTTP和HTTPS端点。诀窍是让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();
};

这是一个旧的答案,只是补充一下。现在有一种更好的方法可以在Azure网站上强制实施HTTPS,如此处所述。 - Akshay Jain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接