HTTPS request, specifying hostname and specific IP address

前端 未结 1 1531
情深已故
情深已故 2021-01-03 03:51

I have a Node.js-based deployment script for my application servers. One step in the deployment process is to validate that these application servers are listening on HTTPS

相关标签:
1条回答
  • 2021-01-03 04:44

    Set the host header for the request:

    const https = require('https');
    
    https.get('https://AA.BB.CC.DD', {
      headers : { host : 'api.example.com' }
    }, res => {
      console.log('okay');
    }).on('error', e => {
      console.log('E', e.message);
    });
    

    EDIT: I dug around a bit to see how this works exactly. To allow HTTPS-based virtual hosting, there's a TLS extension called SNI (Server Name Indication). This extension is used by a client to indicate the hostname to which it is trying to connect, so the server can pick the appropriate certificate that belongs to that hostname.

    Node's tls module, which is used by https, has an option servername to set this hostname:

    https.get('https://AA.BB.CC.DD', {
      servername : api.example.com'
    }, ...)
    

    However, you still need to pass a Host header too (that's part of the regular HTTP protocol):

    https.get('https://AA.BB.CC.DD', {
      headers : { host : 'api.example.com' },
      servername : 'api.example.com'
    }, ...)
    

    To keep things DRY, Node.js will set servername to the Host header, unless it's already set to something else (here).

    0 讨论(0)
提交回复
热议问题