本文介绍的是博主在自己的生产环境中实际部署nginx+tomcat负载均衡的步骤和一些总结,文档仅代表个人目前的想法,如果不足或错误之处还请各位指出。
第一步,相关准备工作,博主的操作系统版本如下(红帽子企业版):
接下来就是准备安装nginx+sticky模块的必备软件包(本文结尾处会给出实际配置过程中用到的软件和配置文件的下载地址,感兴趣的可自行下载测试),主要包含以下:
nginx-1.6.2.tar.gz
nginx-sticky-module-1.1.tar.gz
openssl-1.0.1c.tar.gz
zlib-1.2.8.tar.gz
pcre-8.35.tar.gz
第二步,前提环境的安装,新建文件夹 mkdir /nginx,将上述提到的软件包全部上传至该文件夹中(博主使用的终端环境为xshell+xftp),使用命令tar zxvf ...将软件全部解压(如 tar zxvf zlib-1.2.8.tar.gz),进入解压后的zlib-1.2.8目录执行如下三条命令(pcre软件包同理)
./configure
make
make install
安装openssl略有不同,主要执行以下命令./config --prefix=/usr/local/openssl
./config -t
make
第三步,安装nginx+sticky,新建文件夹mkdir /usr/local/nginx,然后进入之前解压的sticky目录,修改两个文件,修改内容如下(修改原因不详述,网络很多相关资料)
ngx_http_sticky_misc.c 的281行修改如下
原digest->len = ngx_sock_ntop(in,digest->data, len, 1);
改后digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in),digest->data, len, 1);
ngx_http_sticky_module.c文件修改如下第6行添加c语言头文件:
#include <nginx.h>
第340行左右修改(iphp->rrp.current = iphp->selected_peer;)
为:
#if defined(nginx_version) && nginx_version >= 1009000
iphp->rrp.current = peer;
#else
iphp->rrp.current = iphp->selected_peer;
#endif
接着进入之前解压的nginx目录,执行如下操作
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1c --add-module=../nginx-sticky-module-1.1 注意:该命令每个--之前都有空格!
make make install操作过程中可能会遇到类似于这样的错误Nginx: error while loading shared libraries: libpcre.so.1
如果遇到执行以下命令即可
ln -s /usr/local/lib/libpcre.so.1 /lib64/安装完成之后执行以下命令可对nginx进行操作:
启动:/usr/local/nginx/nginx 停止:/usr/local/nginx/nginx -s stop 重新加载配置文件:/usr/local/nginx/nginx -s reload
启动nginx之后再浏览器中看到如下类似界面即表示配置成功。
第四步,配置nginx开机自启动,首先按照官网的教程(官网地址https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/),编写相应脚本,执行vi
/etc/init.d/nginx加入如下脚本:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # 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/nginx/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs 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 sleep 1 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注意:如果安装路径和博主不一致则需要修改标红的两处为自己的路径。
chmod 755 /etc/rc.d/init.d/nginx chkconfig nginx on
启动nginx即可通过service nginx start关闭则为service nginx stop
至此全部安装完成!接下来便是配置nginx.conf的工作,配置nginx.conf启动sticky只需在upstream中加入sticky关键字即可,参考配置文件以及文中提到的软件在下述链接中给出:http://download.csdn.net/download/clevercode/8654953
Linux(Red Hat Enterprise)下安装nginx与sticky模块,并配置开机自启动图文教程
原文:http://blog.csdn.net/syystx/article/details/55250783