Search code examples
node.jsiisdnswindows-server-2012

Node.js: bind port 3000 with DNS in IIS


I'm running a Windows Server 2012 R2 with IIS 8.5 with different ASP apps. Now I developed a Node.js application which listens on port 3000. Also, there's a DNS record which points exactly to this server.

How can I get the node application to run without IIS involved (iisnode is not an option) on the same server? It should still listen on port 3000 (or similar), but when the user enters the domain name wihtout port, it should recognize it and serve the node app.

With a development server (no DNS binding) it simply worked by writing the port at the end of the ip address (like 10.10.10.10:3000). However, with the DNS record, I can't get this to work. sub.domain.com:3000 is not displaying anything and sub.domain.com is redirecting to the IIS start page. What can I do here?


Solution

  • sub.domain.com:3000 should be working. You probably have some firewall blocking incoming external requests to this port.

    When someone types http://sub.domain.com (without a port), the client send the request to port 80 by default (or 443 if https:// is used). If IIS is already listening on port 80, then there is nothing you can do to make node.js respond to this port aswell.

    What you can do is setup a lightning-fast Reverse Proxy (like NGINX) and bind it to port 80. Move IIS to another one, like 8080. Configure NGINX to pass forward requests from port 80 to another port, based on incoming requests' domain.

    Configuration would be something like this:

    server {
        listen 80;
    
        server_name domain.com;
    
        location / {
           proxy_set_header X-Real-IP  $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_set_header Host $host;
           proxy_pass http://127.0.0.1:8080;
        }
    }
    
    server {
        listen 80;
    
        server_name sub.domain.com;
    
        location / {
           proxy_set_header X-Real-IP  $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_set_header Host $host;
           proxy_pass http://127.0.0.1:3000;
        }
    }
    

    You can also use your IIS as the Reverse Proxy, but as you said, you don't want IIS to get involved on the request, so NGINX would be a good option.