apache2反向代理node.js应用

在之前记录的随笔中,只是介绍了怎么在apache2中使用proxy模块,后来查到了一些资料,可以通过下面网址查看配置块的详细参数信息

http://man.ddvip.com/soft/apache2.0/sections.html

在服务器上面部署node.js应用的时候我遇到了下面的问题:

node.js应用监听的是3000端口,使用反向代理可以将80端口的所有请求代理到http://127.0.0.1:3000/去

但是当对node.js做维护的时候,停止监听3000端口是一个很好的选择,但是如果这个时候有人访问你的站点,该怎么办呢?

这个问题是前一段时间遇到的,知道昨天才真正下决心把它解决掉。

ok,我开始寻找解决方式,观察停止监听3000端口时服务器返回的错误代码是503,好了,有思路了,直接重定向503页面就可以了。

于是在Virtualhost *:80中添加了如下代码,是的服务器访问出现503错误的时候,跳转到专有界面

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.localhost
    DocumentRoot "E:/phpworkspace/myblog"
    ServerName www.myblog.lc
    ServerAlias www.myblog.lc
    ErrorLog "logs/dummy-host2.localhost-error.log"
    CustomLog "logs/dummy-host2.localhost-access.log" common
    <Directory "E:/phpworkspace/myblog">
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
    #ProxyPass /article http://127.0.0.1:3000/article
    #ProxyPassReverse /article http://127.0.0.1:3000/article
    ErrorDocument 503 /index.html
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

  本以为问题解决了,进行测试,服务器端停止监听3000端口,访问主机地址,结果出现的还是503错误

于是又去查找资料,仔细看了反向代理的介绍之后,恍然大悟,原来上面设置的503 都会被转到127.0.0.1:3000/index.html

可是我原来的应用下面就没有index.html文件

于是就想 能否在node.js监听的时候去访问3000端口,如果发现没有监听也就是出现503错误的时候访问的是apache默认的目录结构下的页面呢

于是终于找准了地方 在apache的默认www下创建error文件夹 在error里面创建一个index.html文件 用来显示503错误

于是上述配置文件信息修改为下面的内容

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.localhost
    DocumentRoot "E:/phpworkspace/myblog"
    ServerName www.myblog.lc
    ServerAlias www.myblog.lc
    ErrorLog "logs/dummy-host2.localhost-error.log"
    CustomLog "logs/dummy-host2.localhost-access.log" common
    <Directory "E:/phpworkspace/myblog">
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
    #ProxyPass /article http://127.0.0.1:3000/article
    #ProxyPassReverse /article http://127.0.0.1:3000/article
    ErrorDocument 503 /error/index.html
    ProxyPass /error/ ! 对error目录不使用代理
    ProxyPass /error/e !
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

  ok,问题解决,这样在服务器端没有监听3000端口的时候,服务器端会返回apache默认结构下的/error/index.html页面