CentOS 6.5 Nginx安装与负载均衡配置
本实例详细说明了在CentOS 6.5系统中部署Nginx服务器及其负载均衡配置的具体步骤,供读者参考实践。
第一步是获取PCRE库,这是一个使用C语言编写的正则表达式处理函数库。
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost download]# tar zxvf pcre-8.36.tar.gz
第二步需要下载zlib压缩库。
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
第三步需获取SSL支持库。
[root@localhost zlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhost download]# cd openssl-1.0.1c
[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz
第四步则是下载Nginx软件包。
[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz
第五步执行安装操作。
[root@localhost download]mv pcre-8.36 /usr/local/
[root@localhost download]mv zlib-1.2.8 /usr/local/
[root@localhost download]mv openssl-1.0.1c /usr/local/
[root@localhost download]mv nginx-1.2.8 /usr/local/
[root@localhost download]cd /usr/local/
[root@localhost local]# cd pcre-8.36
[root@localhost pcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36] cd ../zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure && make && make install
[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c
[root@localhost openssl-1.0.1c]# ./config && make && make install
[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8
[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install
第六步为启动Nginx服务。
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
第七步进行功能测试。
在确认网络连通性的客户端浏览器中访问安装了Nginx的服务器的IP地址,若页面显示“Welcome to nginx!”,则表明安装与配置已成功完成。

第九步配置系统开机自动启动Nginx,通常通过编写Shell脚本来实现。
[root@localhost logs]# vi /etc/init.d/nginx
向系统中添加相应的Shell启动脚本。
#!/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: /var/run/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=/var/run/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
脚本保存后,需赋予其可执行权限,确保所有用户均可操作该脚本。
[root@localhost ~]# chmod a+x /etc/init.d/nginx
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2417 2416) is running...
[root@localhost logs]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
[root@localhost logs]# /etc/init.d/nginx start
Starting nginx: [ OK ]
[root@localhost logs]# /etc/init.d/nginx status
nginx (pid 2454 2452) is running...
[root@localhost ~]# vi /etc/rc.local
将Nginx服务添加到系统启动项中。
/etc/init.d/nginx start
重启计算机后,配置即可生效。
注意:脚本中定义的启动目录需根据实际安装路径进行调整,若安装位置不同,必须修改对应参数。
接下来进行负载均衡的配置。
实验涉及以下两台服务器环境:
192.168.137.197(作为转发机,已按前述步骤安装Nginx的虚拟机) 192.168.137.33(未安装Nginx,运行测试用Web工程的虚拟机) 10.10.33.59(未安装Nginx,运行测试用Web工程的本地电脑地址) 以上节点均运行有测试用的Web工程。 127.0.0.1 web_app
[root@a conf]# vi /usr/local/nginx/conf/nginx.conf
具体配置展示如下:
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
upstream web_app {
server 192.168.137.197:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.33:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.10.33.59:8080 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
listen 80;
server_name chinaapp.sinaapp.com;
index index.jsp index.html index.htm;
#发布目录/data/www
root /data/www;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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://web_app;
expires 3d;
}
}
}
首先设置Host解析。
[root@a conf]# vi /etc/hosts
在hosts文件中添加相应条目。
127.0.0.1 web_app
随后启动Nginx服务。
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
依次启动三台服务器的Web应用服务。
在IP为192.168.137.197的机器上执行相关指令。
[root@a PROGRAM]# curl "http://web_app/index.jsp"
测试结果呈现如下:

以下是常见问题及对应的解决办法。
1、在安装PCRE过程中提示错误:configure: error: You need a C++ compiler for C++ support.
该问题原因是系统未安装C++编译器,可通过执行以下命令进行安装解决。
[root@localhost pcre-8.37]# yum install -y gcc gcc-c++
2、Nginx启动失败的情况。
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
原因通常是64位系统默认去/usr/local/lib目录下查找库文件,导致无法加载。
检查方法如下:
[root@localhost nginx]# cd /usr/local/
[root@localhost local]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre
这表明缺失的库文件位于lib64目录下。
可通过设置软链接来解决此路径问题。
[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost local]# cd nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3、启动时出现报错信息。
报错内容为:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决办法:sudo fuser -k 80/tcp
该错误表示80端口被其他进程占用,需关闭占用该端口的进程。
希望本文所述内容能对大家配置CentOS服务器有所帮助。