LNMP环境搭建—Nginx负载均衡篇

Posted 21CTO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP环境搭建—Nginx负载均衡篇相关的知识,希望对你有一定的参考价值。

网址:http://blog.csdn.net/beautifulencounter/article/details/45815551


1.Nginx配置文件测试


root@kallen:/usr/local/nginx/sbin# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful


2.Nginx启动


[root@kallen ~]# /usr/local/nginx/sbin/nginx


3.Nginx负载均衡


Nginx Architecture:


Nginx LoadBalance:


Nginx High Availability:



Nginx Access Process:



Nginx 的 upstream 目前支持4 种方式的分配——


(1)轮询(默认) :


每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。


(2)weight :


指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。


(3)ip_hash :



(4)fair(第三方):


按后端服务器的响应时间来分配请求,响应时间短的优先分配。


(5)url_hash(第三方):


按访问url 的hash 结果来分配请求,使每个url 定向到同一个后端服务器,后端服务器为缓存时比较有效。


4.Nginx安装及配置


(1) nginx源码安装


[root@kallen ~]# cd /usr/local/src/


[root@kallen ~]# wget http:

//syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz

[root@kallen ~]# tar zxvf nginx-0.9.6.tar.gz

[root@kallen ~]# cd nginx-0.9.6


./configure --prefix=/usr/local/nginx

--sbin-path=/usr/local/nginx/sbin/nginx

--conf-path=/usr/local/nginx/conf/nginx.conf

--error-log-path=/usr/local/nginx/logs/error.log

--http-log-path=/usr/local/nginx/logs/access.log

--pid-path=/usr/local/nginx/var/nginx.pid

--lock-path=/usr/local/nginx/var/nginx.lock

--http-client-body-temp-path=/dev/shm/nginx_temp/client_body

--http-proxy-temp-path=/dev/shm/nginx_temp/proxy

--http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi

--user=www --group=www

--with-cpu-opt=pentium4F

--without-select_module

--without-poll_module

--with-http_realip_module

--with-http_sub_module

--with-http_gzip_static_module

--with-http_stub_status_module

--without-http_ssi_module

--without-http_userid_module

--without-http_geo_module

--without-http_memcached_module

--without-http_map_module

--without-mail_pop3_module

--without-mail_imap_module

--without-mail_smtp_module

--with-pcre=/usr/local/src/pcre-8.32/

--with-zlib=/usr/local/zlib


[root@kallen ~]# make && make install

[root@kallen ~]# mkdir /dev/shm/nginx_temp


有的nginx版本编译时会因为pcre编译不过去,需要修改一下

--with-pcre=/usr/local/src/pcre-8.32,前提是已经下载了pcre源码包pcre-7.8.tar.gz,并解压到/usr/local/src/pcre-8.32,不需要编译pcre.


在实际安装过程中可能需要手动安装以下依赖包:


a. 安装依赖软件


[root@kallen ~]# apt-get --install-suggests install gcc g++ make


b. 下载相关软件


wget http:

//jaist.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

wget http:

//zlib.net/zlib-1.2.8.tar.gz

wget http:

//www.openssl.org/source/openssl-1.0.1g.tar.gz

wget http:

//www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2

wget http:

//tengine.taobao.org/download/tengine-2.0.2.tar.gz


c. 安装Pcre


tar zxvf pcre-8.35.tar.gz

cd pcre-8.35

./configure --prefix=/usr/local/pcre-8.35

make && make install


d. 安装Zlib


tar zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure --prefix=/usr/local/zlib-1.2.8

make && make install


[ERROR]-1:


./configure: error: the HTTP gzip module requires the zlib library.

You can either disable the module by using --without-http_gzip_module

option, or install the zlib library into the system, or build the zlib library

statically from the source with nginx by using --with-zlib=<path> option


[ERROR]-2:


configure: error: You need a C++ compiler for C++ support.

make[1]: *** [/usr/local/src/pcre-8.32/Makefile] Error 1

make[1]: Leaving directory /home/kallen/MyDOC/nginx-1.8.0

make: *** [build] Error 2


安装完成后的配置信息如下:


[Nginx Configuration Summary]

Configuration summary

+ using PCRE library: /usr/local/src/pcre-8.32

+ OpenSSL library is not used

+ using builtin md5 code

+ sha1 library is not found

+ using zlib library: /usr/local/zlib

nginx path prefix:"/usr/local/nginx"

nginx binary file:"/usr/local/nginx/sbin/nginx"

nginx configuration prefix:"/usr/local/nginx/conf"

nginx configuration file:"/usr/local/nginx/conf/nginx.conf"

nginx pid file:"/usr/local/nginx/var/nginx.pid"

nginx error log file:"/usr/local/nginx/logs/error.log"

nginx http access log file:"/usr/local/nginx/logs/access.log"

nginx http client request body temporary files:"/dev/shm/nginx_temp/client_body"

nginx http proxy temporary files:"/dev/shm/nginx_temp/proxy"

nginx http fastcgi temporary files:"/dev/shm/nginx_temp/fastcgi"

nginx http uwsgi temporary files:"uwsgi_temp"

nginx http scgi temporary files:"scgi_temp"


(2) 编写nginx启动脚本


[root@kallen init.d]# vi /etc/init.d/nginx


写入以下内容:


#!/bin/bash

#

# Startup script for the Nginx HTTP Server

#

# Kallen Ding, Apr 23 2015


NGINX_PATH="/usr/local/nginx/"

OPTIONS="-c ${NGINX_PATH}conf/nginx.conf"

prog=nginx

nginx=${NGINX_PATH}sbin/nginx

pidfile=/var/run/nginx.pid


# Source function library.

. /etc/rc.d/init.d/functions


start() {

echo -n "Starting $prog: "

daemon --pidfile=${pidfile} $nginx $OPTIONS

RETVAL=$?

echo

return $RETVAL

}


stop() {

echo -n "Stopping $prog: "

killproc -p ${pidfile} $nginx

RETVAL=$?

echo

}

reload() {

echo -n $"Reloading $prog: "

killproc -p ${pidfile} $nginx -HUP

RETVAL=$?

echo

}


# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

reload)

reload

;;

status)

status $prog

RETVAL=$?

;;

*)

echo "Usage: $prog {start|stop|restart|reload|status}"

RETVAL=3

esac


exit $RETVAL


保存后,更改/etc/init.d/nginx的权限


[root@kallen ~]# chmod 755 /etc/init.d/nginx

[root@kallen ~]# chkconfig --add nginx

[root@kallen ~]# chkconfig nginx on


关于21CTO社区

21CTO.com是中国互联网第一技术人脉与社交平台。我们为国内最优秀的开发者提供社交、学习等产品,帮助企业快速对接开发者,包括人才招聘,项目研发,顾问咨询服务。

看微信文章不过瘾,请移步到网站,诚挚欢迎您加入社区作者团队。

投稿邮箱:info@21cto.com





以上是关于LNMP环境搭建—Nginx负载均衡篇的主要内容,如果未能解决你的问题,请参考以下文章

NGINX实现负载均衡,并利用PHP实现session入库

nginx实现负载均衡实例

负载均衡环境搭建(nginx和tomcat)

LNMP架构应用实战—Nginx反向代理负载均衡配置

用docker搭建nginx负载均衡测试环境

Nginx负载均衡+Keepalived高可用集群