在VirtualBox(CentOS)中安装并配置Nginx以使用WebSocket的方法备忘录
如果使用node.js的websocket,似乎需要安装一个叫nginx_tcp_proxy_module的模块。而且添加模块的话需要重新编译。真是太糟糕了。所以我决定先通过yum卸载nginx,然后重新编译并安装。
请留意下列事项
- 
- ここから先は全てroo(権限)ユーザーで実行しています
 
- 
- 要git
 
- 開発環境構築の話です。本番環境などはもっと詳しいブログをみつけてください。
有一個參考網站。
- 
- nginx 1.0.10をCentOSでインストール(WebSocketのリバースプロキシ付き)
 
- 
- nginx-setup / nginx_install
 
- node.js用の環境作り – ディレクトリ構成とnginxの設定から起動テストまで
获取nginx的源代码
带回来
cd /usr/local/
wget http://nginx.org/download/nginx-1.3.13.tar.gz
tar zxvf nginx-1.3.13.tar.gz
cd /usr/local/nginx-1.3.13
添加nginx模块
由于有许多方便的事物可供选择,我想加入其中一项。
cd /usr/local/nginx-1.3.13
git clone https://github.com/yaoweibin/nginx_tcp_proxy_module.git
git clone https://github.com/agentzh/headers-more-nginx-module.git
git clone https://github.com/FRiCKLE/ngx_cache_purge.git
应用nginx_tcp_proxy_module补丁
如果没有这个模块,就无法使用websocket。
这是我第一次打补丁,只需要输入命令。
patch -p1 < nginx_tcp_proxy_module/tcp.patch 
编译所需的模块添加
如果不够,我们可以补充进去。
yum install -y patch
yum install -y pcre-devel
yum install -y zlib-devel openssl-devel
用configure进行编译,然后使用make和make install安装。
和往常一样
./configure --prefix='/usr/local/nginx' --with-http_ssl_module --with-http_stub_status_module --with-http_xslt_module --with-http_realip_module --with-http_gzip_static_module --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --with-debug --add-module=./headers-more-nginx-module --add-module=./ngx_cache_purge --add-module=./nginx_tcp_proxy_module
make
make install
创建启动命令
我参考了在CentOS上安装带有WebSocket反向代理的Nginx 1.0.10的方法。
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
#nginx="/usr/local/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
修改conf的设定
我已删除了各种注释。由于是本地环境,所以使用最简配置。
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #ここからの設定がapacheでいうvirtualhostの設定の項目になります。
    #rootがapacheでいうドキュメントルート
    #server_nameに/etc/hostsで書いたvirtulahostと解釈して変更してください。
    #lo.test.org server directive
    server {
        listen       80;
        server_name  lo.test.org;
        location / {
            root   /home/web/lo.test.org/www;
            index  index.html index.htm;
        }
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        #nginxでphpを使いたい場合の設定です。fastcgi_passはphp-fpmを起動しているポート番号を指定してください。(デフォルトは9000で起動している)
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/web/lo.test.org/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    #lo.sample.org server directive
    server {
        listen       80;
        server_name  lo.sample.org;
        root   /home/web/lo.sample.org/www;
        index  index.php index.html index.htm;
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/web/lo.sample.org/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
确认nginx是否已启动
只需执行编写的启动脚本
/etc/init.d/nginx start
nginx を起動中:                                            [  OK  ]
设置使得nginx在启动时自动运行。
让我们在chkconfig中添加设置。
chkconfig --add nginx
chkconfig nginx on
以上。起初我觉得修补之类的事情可能会很麻烦,但没想到还挺顺利的,真是太好了。
 
    