Apache2 代理 node.js伺服器

這邊有兩個步驟

  1. 往常我要連入node.js時的網址是http://….:3000/,現在要把它改成http://…./nodejs/
  2. 永遠保持node.js的伺服器開啟

開始之前我們要安裝環境。

a2enmod proxy
a2enmod proxy_http

然後是改網址的部分,這邊我們要動到apache2的設定,所以首先打開 /etc/apache2/sites-available/000-default.conf,我們要加入一個重新導向的部分。

 <Location /nodejs>
     ProxyPass http://127.0.0.1:3000
     ProxyPassReverse http://1127.0.0.1:3000
 </Location>

加入後長的是以下這樣。

#/etc/apache2/sites-available/000-default.conf
 
    # The ServerName directive sets the request scheme, hostname and port that 
    # the server uses to identify itself. This is used when creating 
    # redirection URLs. In the context of virtual hosts, the ServerName 
    # specifies what hostname must appear in the request's Host: header to 
    # match this virtual host. For the default virtual host (this file) this 
    # value is not decisive as it is used as a last resort host regardless. 
    # However, you must set it for any further virtual host explicitly. 
    #ServerName www.example.com
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
    # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular 
    # modules, e.g. #LogLevel info ssl:warn
    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf
    ######    
          
     <Location /nodejs>
     ProxyPass http://127.0.0.1:3000
     ProxyPassReverse http://1127.0.0.1:3000
     </Location>  
    

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

然後重啟。

sudo /etc/init.d/apache2 restart

這樣就完成了。

那再來就是永遠保持node.js的伺服器開啟,這邊選用的是forever,一樣我們必須先灌環境。

$ sudo npm install forever -g

然後啟動

forever start app.js

如果是express的情況

forever start ./bin/www.js

然後有幾個會用到的指令

#formate
forever stopall

#view logs
forever logs 0

#detail
forever list

#restart
forever restart

 

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。