NodeJS HTTP - listen on other port than 80

后端 未结 6 874
情歌与酒
情歌与酒 2021-01-12 00:21

I am running XAMPP on Windows to host an Apache server on port 80. Now I\'m trying to have a NodeJS script running in the background but the problem is that it can only list

6条回答
  •  离开以前
    2021-01-12 01:11

    Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:

    I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.

    This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:

    I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:

    +---------+------+-------------------------------------+
    | Backend | Port |             Web Address             |
    +=========+======+=====================================+
    | Apache  |   80 | 100.120.110.43 or 100.120.110.43:80 |
    | Node1   | 8080 | 100.120.110.43:8080                 |
    | Node2   | 8081 | 100.120.110.43:8081                 |
    +---------+------+-------------------------------------+
    

    If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:

    ProxyPass "/node1"  "http://100.120.110.43:8080/"
    ProxyPassReverse "/node1"  "http://100.120.110.43:8080/"
    
    ProxyPass "/node2"  "http://100.120.110.43:8081/"
    ProxyPassReverse "/node2"  "http://100.120.110.43:8081/"
    

    The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.

    Listen 80
    
        DocumentRoot /www/apache
        ServerName www.apachefrontend.com
        ProxyRequests off
        ProxyPass /node1  http://0.0.0.0:8080/
        ProxyPassReverse /node1  http://0.0.0.0:8080/
        ProxyPass /node2  http://0.0.0.0:8081/
        ProxyPassReverse /node2  http://0.0.0.0:8081/
    
    

    You could also point to different hosts/ips that only you have access to.

    Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.

    Listen 80
    
        DocumentRoot /www/apache
        ServerName www.apachefrontend.com
    
    
        ServerName www.nodebackend1.com
        ProxyRequests off
        
            Order deny,allow
            Allow from all
        
        
            ProxyPass /  http://0.0.0.0:8080/
            ProxyPassReverse /  http://0.0.0.0:8080/
        
    
    
        ServerName www.nodebackend2.com
        ProxyRequests off
        
            Order deny,allow
            Allow from all
        
        
            ProxyPass /  http://0.0.0.0:8081/
            ProxyPassReverse /  http://0.0.0.0:8081/
        
    
    

    For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.

    These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.

提交回复
热议问题