负载均衡使用nginx sticky实现session共享

nginx-sticky-module第三方模块主要用于基于cookie实现会话保持。

nginx sticky 模块工作流程图

下载nginx sticky

安装nginx+stiky

# wget http://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gz
# tar -xzvf nginx-sticky-module-1.1.tar.gz
# wget http://nginx.org/download/nginx-1.8.1.tar.gz
# tar -xzvf nginx-1.8.1.tar.gz
# cd nginx-1.8.1
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=../nginx-sticky-module-1.1
# make
# make install

执行./configure的过程中可能会遇到以下错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.

执行该命令解决:

yum -y install pcre-devel

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.

执行该命令解决:

yum -y install openssl openssl-devel

在执行make的时候会报错:
../nginx-sticky-module-1.1/ngx_http_sticky_misc.c:281:2: error: passing argument 2 of ‘ngx_sock_ntop’ makes integer from pointer without a cast [-Werror]
digest->len = ngx_sock_ntop(in, digest->data, len, 1);

解决办法:

cd ../nginx-sticky-module-1.1
vim 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);

配置nginx sticky

upstream dispatch {
    sticky;
    server 192.168.100.100:8080;
    server 192.168.100.102:8080;
}
server {
        listen       80;
        server_name  abswolf.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://dispatch;
        }
    }

Nginx配置启动脚本和开机启动

vim /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
chkconfig --add nginx #添加服务
chkconfig nginx on #开机启动

Nginx反向代理配置

先说需求。一台服务器上有多个web容器,apache、nginx、tomcat。由于每个容器都需要监听不同的端口。所以为了用户访问方便,带个端口访问是在是太不专业了。使用Nginx来方向代理,至于原理就不说明了,这里只说如何配置:

Nginx默认监听80端口,假设apache监听81、tomcat监听82端口。那么我们要跳转的域名是apache.06peng.com,tomcat.06peng.com。

server {
        listen       80;
        server_name  apache.06peng.com;

        location / {
        	proxy_redirect off;
   		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:81;
        }        
    }
    
    server {
        listen       80;
        server_name  tomcat.06peng.com;
        location / {
        	proxy_redirect off;
   		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:82;
        }     
    }