原理在 那里面基本上已经介绍完了,下面直接开始配置 安装配置第三方模块,实现upstream中对后端http server的健康状态检测: yum groupinstall "Development Libraries" "Development Tools" yum install pcre-devel -y wget
[root@localhost src]#tar xf cep21-healthcheck_nginx_upstreams-16d6ae7.tar.gz [root@localhost src]# mv cep21-healthcheck_nginx_upstreams-16d6ae7 healthcheck_nginx_upstreams [root@localhost src]#tar xf nginx-1.0.14.tar.gz 对nginx打补丁 [root@localhost src]#cd nginx-1.0.14 [root@localhost nginx-1.0.14]# patch -p1 < ../healthcheck_nginx_upstreams/nginx.patch patching file src/http/ngx_http_upstream.c Hunk #1 succeeded at 4296 (offset 3 lines). patching file src/http/ngx_http_upstream.h Hunk #1 succeeded at 110 (offset 1 line). patching file src/http/ngx_http_upstream_round_robin.c Hunk #1 succeeded at 5 (offset 1 line). Hunk #3 succeeded at 36 (offset 1 line). Hunk #5 succeeded at 390 (offset 1 line). Hunk #7 succeeded at 497 (offset 1 line). Hunk #9 succeeded at 633 (offset 1 line). patching file src/http/ngx_http_upstream_r 而后编译nginx,在执行configure时添加类似下面的选项: --add-module=/usr/local/src/healthcheck_nginx_upstreams [root@localhost nginx-1.0.14]# groupadd -r nginx [root@localhost nginx-1.0.14]# useradd -r -g nginx -s /bin/false -M nginx [root@localhost nginx-1.0.14]# ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/usr/local/src/nginx/client/ \ --http-proxy-temp-path=/var/usr/local/src/nginx/proxy/ \ --http-fastcgi-temp-path=/var/usr/local/src/nginx/fcgi/ \ --with-pcre \ --add-module=/usr/local/src/healthcheck_nginx_upstreams/ ######./configure 结果如下##### Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library
nginx path prefix: "/usr" nginx binary file: "/usr/sbin/nginx" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/usr/local/src/nginx/client/" nginx http proxy temporary files: "/var/usr/local/src/nginx/proxy/" nginx http fastcgi temporary files: "/var/usr/local/src/nginx/fcgi/" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@localhost nginx-1.0.14]# make && make install [root@localhost nginx-1.0.14]# vim /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/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 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 } 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 [root@localhost nginx-1.0.14]# chmod +x /etc/init.d/nginx [root@localhost nginx-1.0.14]# chkconfig --add nginx [root@localhost nginx-1.0.14]# service nginx start Starting nginx: [ OK ] [root@localhost nginx-1.0.14]# cd /etc/nginx/ [root@localhost nginx]# mv nginx.conf nginx.conf.bak [root@localhost nginx]#vim nginx.conf user nginx; worker_processes 2;
#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;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on; upstream { server 192.168.1.117 weight=5; server 192.168.1.118 max_fails=3 fail_timeout=30s; server 192.168.1.189:8080; } server { listen 80; server_name ; access_log /var/log/www.gabylinux.com/access.log; error_log /var/log/www.gabylinux.com/error.log debug; #set your default location location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass ; } }
} [root@localhost nginx]# mkdir -p /var/log/www.gabylinux.com/ [root@localhost nginx]# service nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful Reloading nginx: [ OK ]
这时候我们用ie浏览器访问192.168.1.189,能够发现访问到不同realserver上,如果其中一台realserver宕机的话,nginx会自动把他踢掉。 生产环境中可以优化上面参数,以提高性能。