在 CentOS 上使用 Apache 的 Proxy mode 使 Node.js 和 Apach

最近用 Node.js 开发一个微信公共平台接口,因为微信公众平台接口配置目前仅支持默认的 80 端口,而个人 CentOS 服务器已经在使用 Apache 运行一些其余业务了。这样就必需要求 Node.js 可以和 Apache 共用 80 端口。html

咱们可使用 Apache 的 Proxy mode 来解决这个问题。node

假设咱们提供给微信公共平台接口配置的地址是:http://www.foobar.com/weixin/receivetoken。那么咱们须要首先给 Apache 配置一个 vhost,域名就是 foobar.com;而后确认 Apache 加载了相应的 Proxy 模块;使 Apache 从新加载配置文件,最后肯定一下咱们的 Node.js 应用要监听的服务端口,好比 11342,启动咱们的 Node.js 应用进行测试。那么咱们经过以下几步配置便可实现。shell

检查 Apache 配置文件

首先找到并打开 Apache 的配置文件,检查一下 Proxy 模块是否已经被加载,比方在个人 CentOS 上配置文件的路径是: /etc/httpd/conf/httpd.conf
apache

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

新增一个 VHOST 配置

而后打开 vhost.conf 为 foobar.com 新增一个 virtual host 配置,在个人 CentOS 上的路径是:/etc/httpd/conf.d/vhost.conf 假设咱们须要和 Apache 共享 80 端口的 Node.js 应用目录为:/srv/www/foobar.com/public_html/weixin,咱们但愿 Node.js 自己运行于 11342 端口。那么咱们应该以下增长 vhost 配置:  浏览器

<VirtualHost *:80>
  ServerAdmin admin@gmail.com
  ServerName foobar.com
  ServerAlias www.foobar.com

  ProxyRequests off

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  <Location />
    ProxyPass http://localhost:11342/
    ProxyPassReverse http://localhost:11342/
  </Location>

  DocumentRoot /srv/www/foobar.com/public_html/weixin
  <Directory "/srv/www/foobar.com/public_html/weixin">
    AllowOverride All
  </Directory>

  ErrorLog /srv/www/foobar.com/logs/error.log
  CustomLog /srv/www/foobar.com/logs/access.log combined
</VirtualHost>


2014.01.14 批注:”allow,deny” 逗号后面不能有空格,不然 httpd 跟配置就匹配不上了,会报错,启动不了。服务器

从新加载 Apache 配置文件及测试

让 Apache 从新加载一下咱们的配置:微信

service httpd reload微信公众平台

最后运行一下 Node.js 脚本,而后就能够在浏览器中测试一下是否工做正常了。ide

注意:wordpress

  1. 上面咱们是以 CentOS 为例说明 Apache 配置文件的路径,以及相关命令;若是是其余 Linux 的发行版本,按照相应平台进行处理便可,重点仍是配置文件自己的内容。

  2. ProxyPass http://localhost:11342/ 及其下面一行最后的“/”都是须要的。

本文地址:http://www.tfan.org/nodejs-running-on-port-80-with-apache

参考资料:

相关文章
相关标签/搜索