Nginx负载均衡服务器配置教程

说明:
Nginx负载均衡服务器
系统:CentOS 5.5
IP:10.15.72.73
Web服务器列表:
Web1:10.15.72.177
Web2:10.15.72.74

实现目的:
用户访问10.15.72.73服务器时,通过Nginx负载均衡到Web1和Web2服务器

推荐阅读:

Nginx Tomcat 集群负载均衡解决笔记

Nginx 配置轮询分流-实现负载均衡【测试通过】

Nginx负载均衡引起的网站不可用

在Linux上使用Nginx为Solr集群做负载均衡

Nginx 实现MySQL的负载均衡

下面配置Nginx负载均衡服务器

准备篇:
一、配置好IP、DNS 、网关,确保使用远程连接工具能够连接服务器
二、配置防火墙,开启80端口
vi /etc/sysconfig/iptables  #编辑防火墙配置文件,添加以下内容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
特别提示:很多网友把这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面
添加好之后防火墙规则如下所示:
#####################################################################
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
#####################################################################
/etc/init.d/iptables restart#最后重启防火墙使配置生效
三、关闭SELINUX
vi /etc/selinux/config  #编辑配置文件
#SELINUX=enforcing#注释掉
#SELINUXTYPE=targeted#注释掉
SELINUX=disabled#增加
:wq#保存
shutdown -r now#重启系统
四 、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
五、下载软件包
1、下载nginx(目前稳定版)

2、下载pcre (支持nginx伪静态)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
3、下载ngx_cache_purge(清除指定URL缓存,方便以后扩展配置nginx缓存服务器)

六、安装编译工具及库文件(使用CentOS yum命令安装,安装的包比较多,方便以后配置lnmp环境)
yum install make apr* autoconf automake curl curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat*  cpp glibc libgomp libstdc++-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch
安装篇
以下是用putty工具远程登录到服务器,在命令行下面操作的
安装 nginx
groupadd  www #添加www组
useradd -g www www -s /bin/false #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统cd /usr/local/src #进入安装目录
tar  zxvf  ngx_cache_purge-1.5.tar.gz #解压
tar  zxvf nginx-1.0.15.tar.gz #解压
cd nginx-1.0.15
./configure --prefix=/usr/local/nginx --without-http_memcached_module
--user=www --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.30  --add-module=../ngx_cache_purge-1.5 #配置
注意:--with-pcre=/usr/local/src/pcre-8.30指向的是源码包解压的路径,而不是安装的路径,否则会报错
make #编译
make install#安装
/usr/local/nginx/sbin/nginx #启动Nginx
=======================================================
设置nginx开启启动
vi /etc/rc.d/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: /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=/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 /usr/local/nginx/logs/nginx.pid
}
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
=======================================================
:wq!#保存退出
chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限
chkconfig nginx on  #设置开机启动
/etc/rc.d/init.d/nginx restart#重启Nginx
=======================================================

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/130e218bb6da6e8cb38c5603df0e24f2.html