简体   繁体   English

使用node-http-proxy从NodeJS到Apache的反向代理不起作用

[英]Reverse proxy from NodeJS to Apache using node-http-proxy does not work

While experimenting with node.js on my vps I stumbled across a problem I can not seem to get fixed. 在我的vps上试验node.js时,我偶然发现了一个似乎无法解决的问题。

I have an apache server running alongside node.js on ubuntu 13.04. 我在Ubuntu 13.04上有一个与node.js一起运行的apache服务器。 My idea was to create a reverse proxy using node-http-proxy. 我的想法是使用node-http-proxy创建反向代理。 Enough information about this can be found, but I seem to have an error that I can not find. 可以找到关于此的足够的信息,但是我似乎有一个找不到的错误。

First I changed the apache ports: 首先,我更改了apache端口:

NameVirtualHost *:9000
Listen 9000

Then I have my hello.js file: 然后我有我的hello.js文件:

var http = require('http');
var httpProxy = require('http-proxy');

var options = {
        router: {
                'somesite.com': '127:0:0:1:9000'
        }
}

var proxyServer = httpProxy.createServer(options).listen(80);

console.log("Proxy server running!");

and finally the apache somesite.com config: 最后是apache somesite.com配置:

<VirtualHost *:9000>
        ServerName somesite.com
        ServerAdmin info@somesite.com
        ServerAlias www.somesite.com

        DocumentRoot /var/www/somesite.com/public_html
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have a basic html file in public_html to make sure everything is working. 我在public_html中有一个基本的html文件,以确保一切正常。 When I start both servers and go to somesite.com then I just see a blank wepbage (if I inspect source there is nothing, instead of my own file). 当我同时启动两个服务器并访问somesite.com时,我只会看到空白的webbage(如果我检查源代码,则没有任何东西,而不是我自己的文件)。

If I start a simple node server on another port and route traffic to that port, that works, so I'm assuming my problem is apache related, but I can't figure out what is wrong. 如果我在另一个端口上启动一个简单的节点服务器并将流量路由到该端口,则可以正常工作,因此我假设我的问题与apache相关,但我无法弄清楚出了什么问题。

Edit 1: Apparently my proxy only worked for somesite.com and not www.somesite.com. 编辑1:显然我的代理服务器仅适用于somesite.com,而不适用于www.somesite.com。 If I go to somesite.com I get: 如果我去somesite.com我会得到:

 An error has occurred: {"code":"EINVAL","errno":"EINVAL","syscall":"connect"}

Edit 2: If I check the access and error log from apache it seems it never receives anything. 编辑2:如果我从apache检查访问和错误日​​志,似乎它什么也没收到。 No messages of me to be found 找不到我的消息

Edit 3: I changed the port on which nodejs is listening, thinking that maybe the running as root has something to do with it, but even running on a random high port, it is not working. 编辑3:我更改了nodejs监听的端口,以为以root身份运行可能与此有关,但是即使在随机高端口上运行,它也无法正常工作。 It can't seem to make a connection between itself and the apache socket (which works perfectly when I directly browse to it). 它似乎无法在自己和apache套接字之间建立连接(当我直接浏览到apache套接字时,它可以很好地工作)。

Apparently replacing 127.0.0.1 with localhost in the hello.js script did the trick. 显然在hello.js脚本中用localhost替换了127.0.0.1可以解决问题。 Not completely sure why (I guess it has something to do with my hosts file), but it works. 不能完全确定为什么(我想这与我的主机文件有关),但是可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM