1. Prepare the lab environment
2. Install nginx server nginx1 and nginx2)
3. Install FastCgi Server
4. Install the http server for static servers)
5. Test whether nginx achieves load balancing and dynamic/static Separation
6. Configure Nginx high-availability Service
1. Prepare the lab environment
1. IP address planning
VIP: 172.16.10.8
Nginx1: 172.16.10.1
Nginx2: 172.16.10.2
Php1: 172.16.10.3
Php2: 172.16.10.4
Web: 172.16.10.6
2. Network Topology
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/05424A157-0.png "title =" image 3.png" alt = "212133877.png"/>
3. Server Configuration
Nginx1 Server
sed -i 's@\(HOSTNAME=\).*@\1nginx1.xiaodong.com@g' /etc/sysconfig/networkecho "172.16.10.2 nginx1.xiaodong.com nginx2" >> /etc/hostsssh-keygen -t rsassh-copy-id .ssh/id_rsa.pub ngix2
Nginx2 Server
sed -i 's@\(HOSTNAME=\).*@\1nginx2.xiaodong.com@g' /etc/sysconfig/networkecho "172.16.10.1 nginx1.xiaodong.com nginx1" >> /etc/hostsssh-keygen -t rsassh-copy-id .ssh/id_rsa.pub ngix2
2. Install nginx server nginx1 and nginx2)
[root@nginx1 ~]# tar xf nginx-1.4.2.tar.gz -C /usr/local/[root@nginx1 ~]# cd /usr/local/[root@nginx1 local]# groupadd -r nginx[root@nginx1 local]# useradd -r -g nginx nginx[root@nginx1 nginx-1.4.2]# cd nginx-1.4.2/[root@nginx1 nginx-1.4.2]# ./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/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre[root@nginx1 nginx-1.4.2]# make && make install[root@nginx1 nginx-1.4.2]# 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 2esac[root@nginx1 nginx-1.4.2]# chmod +x /etc/init.d/nginx[root@nginx1 nginx-1.4.2]# service nginx start
Note: Some packages may be missing during installation, but you don't have to worry about it. You only need to use yum install to solve the problem.
1. nginx supports nginx and nginx2 in php)
[root@nginx1 ~]# vim /etc/nginx/fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;~
2. Modify nginx configuration file nginx1, nginx2) to implement static/dynamic separation and record the visitor's IP address
orker_processes 2;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; proxy_cache_path /data/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g; client_max_body_size 20m; client_header_buffer_size 16k; large_client_header_buffers 4 16k; tcp_nopush on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_proxied any; gzip_http_version 1.1; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; proxy_temp_path /tmp/proxy_temp; proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g; proxy_connect_timeout 50; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 128k; proxy_buffers 16 256k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 1024m; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504; upstream web { server 172.16.10.3:9000 max_fails=3 fail_timeout=30s; server 172.16.10.4:9000 max_fails=3 fail_timeout=30s; server 172.16.10.1:80 backup; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root /web/htdoc; fastcgi_pass web; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; proxy_set_header X-Real-IP $remote_addr; } location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { proxy_pass http://172.16.10.6; proxy_set_header X-Real-IP $remote_addr; proxy_cache STATIC; proxy_cache_valid 200 1d; proxy_cache_valid 301 302 10m; # proxy_cache_vaild any 1m; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } }}
Note:
Row 10th-Row 18: Enable the Proxy Cache Function
Lines 3-26: Enable the compression function
Lines 5-51: dynamic web page forwarding
Row 50th: Modify the header so that the backend web server can see the address of the access end.
Lines 3-56: Forwarding static webpages
3. Install FastCgi Server
1. php1 and php2 servers
[root@php1 ~]#yum install gcc libxml2-devel openssl-devel bzip2-devel libmcrypt-devel -y[root@php1 ~]# tar xf php-5.4.19.tar.bz2[root@php1 ~]# cd php-5.4.19[root@php1 php-5.4.19]# ./configure --prefix=/usr/local/httpd/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2[root@php1 php-5.4.19]# make && make install
2. Provide the configuration files php1 and php for php2)
[root@php1 php-5.4.19]# cp /usr/local/httpd/php/etc/php-fpm.conf.default/usr/local/httpd/php/etc/php-fpm.conf
[root@php1 php-5.4.19]# cp php.ini-production /etc/php.ini
3. Provide the Sysv init script for php-fpm and add it to the service list php1 and php2)
[root@php1 php-5.4.19]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm[root@php1 php-5.4.19]# chmod +x /etc/rc.d/init.d/php-fpm[root@php1 php-5.4.19]# chkconfig --add php-fpm[root@php1 php-5.4.19]# chkconfig php-fpm on
4. modify the configuration files php1 and php2)
[root@php1 ~]# vim /usr/local/httpd/php/etc/php-fpm.conflisten = 172.16.10.3:9000
5. Start the php1 and php2 services)
root@php1 php-5.4.19]# service php-fpm start
6. Create the php URL directory php1)
[root@php1 ~]# mkdir -pv /web/htdoc/[root@php1 ~]# vim /web/htdoc/index.php
7. Create the php URL directory php2)
[root@php2 ~]# mkdir -pv /web/htdoc/[root@php2 ~]# vim /web/htdoc/index.php
4. Install the http server for static servers)
[root@http ~]# yum install httpd -y[root@http ~]#echo "
5. Test whether nginx achieves load balancing and dynamic/static Separation
1. Test the dynamic page access function
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0542462157-1.png "style =" float: none; "title =" image 9.png" alt = "200202922.png"/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/05424621Z-2.png "style =" float: none; "title =" image 10.png" alt = "200206459.png"/>
2. Test the access to the static page
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0542461P5-3.png "title =" image 11.png" alt = "200252219.png"/>
In this case, although the dynamic and static separation is achieved after Nginx load balancing, but the high availability of the nginx server cannot be guaranteed, the following configuration of nginx High Availability
6. Configure Nginx high-availability Service
1. Install heartbeatnginx1 and nginx2)
[root@nginx1 ~]# yum install heartbeat -y
2. Copy module files
[root@nginx1 ha.d]# cd /usr/share/doc/heartbeat-3.0.4/[root@nginx1 heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/
Note:
Authkeys # the authentication key file between nodes
Ha. cf # Master configuration file of heartbeat
Haresources # Cluster Resource Management Configuration File
3. Modify the authkeys configuration file
[Root @ nginx1 ha. d] # openssl rand-hex 8>/etc/ha. d/authkeys: generate a random number [root @ nginx1 ha. d] # vim authkeysauth 2 # 1crc #2sha1 HI! #3md5 Hello! 2sha1 07cc87ff210e92e0
4. Modify permissions
[root@nginx1 ha.d]# chmod 600authkeys
5. Modify the master configuration file
[Root @ nginx1 ha. d] # vim ha. cflogfile/var/log/ha-logkeepalive 2 deadtime 30 warntime 10 ucast eth0 172.16.10.2 # point to nginx2 IPnode nginx1.xiaodong. comnode restart
6. Modify the resource configuration file
[root@nginx1 ~]# vim /etc/ha.d/haresourcesngnix1.xiaodong.com 172.16.10.8/16/eth0 nginx
Note: nginx1 is the primary node.
7. Copy the configuration file to nginx2
[root@nginx1 ~]# cd /etc/ha.d/[root@nginx1 ha.d]# scp -p authkeys haresources ha.cf nginx2:/etc/ha.d/
8. Start the heartbeat Service
[root@nginx1 ~]# service heartbeat start[root@nginx2 ~]# service heartbeat start
9. Test whether heartbeat and nginx are combined
View the startup log of nginx1
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0542464001-4.png "title =" image 1.png" alt = "210931144.png"/>
10. Stop the nginx1 Service
[root@nginx1 ~]# service heartbeat stop
When nginx1 is stopped, view the nginx2 log information
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0542461H0-5.png "title =" image 2.png" alt = "210953889.png"/>
The above information is reported. After nginx1 is down, nginx2 immediately detects and starts the nginx service to ensure the high availability of nginx.
This blog has ended since now. Hope you can give me more advice !!!
This article is from the "small building-HOME" blog, please be sure to keep this source http://xiaodong88.blog.51cto.com/1492564/1297954