CentOS6--Nginx安装与配置(负载均衡)

一、  安装环境包
1、首先使用yum命令安装、升级所需的程序库。Nginx的配置及运行需要pcre、zlib、 openssl等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提供相应的库和头文件,确保Nginx的安装顺利完成。在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件

# yum -y install gcc-c++
# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

 二、 安装nginx

Nginx官网:http://nginx.org/

yum方式安装nginx

1、检查系统是否安装的Nginx

# find -name nginx

2、安装nginx的最新稳定版yum源,Nginx最新版yum源在EPEL RPM包中,wget下载安装即可

#cd  /root

# wget –O http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

如果没安装wget小工具,使用如下命令安装:

# yum -y install wget
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm //按路径安装并显示进度

3、安装nginx

# yum install nginx

4、启动测试nginx

# service nginx restart

Nginx启动后有两个进程,master为主进程,worker为工作进程

在启动完NGINX后,我们可以在浏览器中输入http://localhost看到welcometo nginx即算成功。

Nginx默认被安装在/usr/local/nginx

tar方式安装nginx
上传或者wgethttp://nginx.org/download/nginx-1.6.3.tar.gz下载一个tar包到/usr/local 
先添加执行Nginx的用户和组(yum不需要这一步)
groupadd nginx
useradd -g nginxnginx -s /bin/false
cd/usr/local/src
tar zxvfnginx-1.6.2.tar.gz
cd nginx-1.6.2
[root@localhostlocal]# tar -zxf nginx-1.6.3.tar.gz
[root@localhostlocal]# cd nginx-1.6.3
[root@localhostnginx-1.6.3]# ./configure--prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module
(相当于:#useradd -s /sbin/nologinnginx
#chown -R nginx: nginx /usr/local/tomcat7)
[root@localhost nginx-1.6.3]#make && make install
[root@localhostsbin]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin建立软链接
注:配置前可以参考:./configure --help给出说明
--prefix:设定Nginx的安装目录
--user和—group:指定Nginx运行用户和组
--with-http_stub_status_module:启用http_stub_status_module模块以支持状态统计

三、nginx启动、停止

1、 Nginx服务器的负载均衡、缓存与动静分离配置。

      配置文件:

#vi /usr/local/nginx/conf/nginx.conf

upstream要写在Server块的外面,可以有多个,名称不同即可,如下:

upstream webserver {
        server  192.168.0.201;
        server  192.168.0.202;
}

server {
        server_name  hfnginx.chinacloudapp.cn;
        access_log  logs/host.access.log  main;
        location / {   //首页负载之后端服务器
            proxy_pass  http://webserver;  //通过upstrean定义的服务器组名调用后端服务器
            proxy_set_header X-Real-IP $remote_addr;  //传递客户端的ip地址
        }
        location ~* ^/form {   //后端Web服务器要有此目录
            proxy_pass  http://webserver;
            proxy_set_header X-Real-IP $remote_addr;
        }
}   

1.1、nginx常用的几种负载方式:

1.1.1、轮询 round-robin(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 

upstream backserver { 
    server 192.168.12.33; 
    server 192.168.12.34; 
} 

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

upstream backserver { 
    server 192.168.12.33 weight=10; 
    server 192.168.12.34 weight=10; 
} 

1.1.3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 

upstream backserver { 
    ip_hash; 
    server 192.168.0.14:88; 
    server 192.168.0.15:80; 
} 

1.1.4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。 

upstream backserver { 
    server server1; 
    server server2; 
    fair; 
} 

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

upstream backserver { 
    server squid1:3128; 
    server squid2:3128; 
    hash $request_uri; 
    hash_method crc32; 
} 

1.1.6、backup服务器

upstream webserver {
        server  192.168.0.201 weight=1 max_fails=2  fail_timeout=2;
        server  192.168.0.202 weight=1 max_fails=2  fail_timeout=2;
        server 127.0.0.1:9008 backup; #调用backup服务器,可以是本机或其他服务器。
}
server {
        listen 80;
        server_name localhost;
        root html/error;
        index index.html;
}

# vi html/error/index.html  //backup服务器的内容
Error Page!

1.2、实现动静分离:

upstream web {
    server  192.168.0.1 weight=1 max_fails=2  fail_timeout=2;
    server  192.168.0.2 weight=1 max_fails=2  fail_timeout=2;
} 

upstream image  {
    server  192.168.0.3 weight=1 max_fails=2  fail_timeout=2;
    server  192.168.0.4 weight=1 max_fails=2  fail_timeout=2;
} 

upstream php {
    server  192.168.0.5 weight=1 max_fails=2  fail_timeout=2;
    server  192.168.0.6 weight=1 max_fails=2  fail_timeout=2;
} 

location  /{
    root html/web;
    index  index.php index.html;
}

location ~* \.php$ {
    fastcgi_proxy  http://php;
}

location ~* "\.(.jpg|png|jpeg|gif)" {
    proxy_pass http://image;
}

//1.3、实现数据缓存(没有考究过是否能实现)

缓存是缓存nginx服务器接收请求过的数据,数据超时时间不能太长,因为数据可能会发生变化,但是nginx服务器内部的缓存的数据还没有更细,会导致客户端请求的数据不是最新数据的问题,数据缓存目录不能定义在server快内,要定义在http块中。

[root@hfnginx nginx]# grep -v "#" conf/conf.d/hfnginx.conf    | grep -v "^$"
upstream webserver {
        server  192.168.0.201 weight=1 max_fails=2  fail_timeout=2;
        server  192.168.0.202 weight=1 max_fails=2  fail_timeout=2;
}
server {
                listen 9008;
                server_name localhost;
                root html/error;
                index index.html;
}

proxy_cache_path  /nginx/cache/first levels=1:2  keys_zone=first:20m max_size=1g; #缓存目录不能定义在server块内,要定义在http块中

#/nginx/cache/first定义缓存目录参数
#evels=1:2  定义两层目录,第一层一个字符名称,第二个两个字符名称
#keys_zone=first:20m 每个缓存都是一个共享内存空间。这就是用户定义共享内存空间地址的名称
#max_size=1g 定义目录最大空间为1g,因为缓存空间越大搜索数据越慢,因此不宜太大。
server {
        server_name  hfnginx.chinacloudapp.cn;
        location / {
            add_header X_Via $server_addr;  #添加服务器地址到报文头部
            add_header X-Cache $upstream_cache_status;  #添加缓存状态到报文头部
            proxy_pass  http://webserver;
            proxy_cache  first; #调用缓存
            proxy_cache_valid 200 10m; #定义缓存失效时间,200是状态码,即缓存状态码是200请求成功的数据,10m是10分钟,即缓存的数据的超时时间10分钟,10分钟后即过期,不定义则缓存不生效            
        }

        location ~* ^/form {
            proxy_cache  cache_one;
            proxy_pass  http://webserver;
            proxy_set_header X-Real-IP $remote_addr;
        }
}

2、启动、停止nginx:

# /usr/localsbin/nginx  //启动

# /usr/local/sbin/nginx -s stop //停止

# /usr/local/nginx/sbin/nginx -s reload //重启

2.1、启动

2.1.1、指定启动文件并启动:

# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2.1.2、默认启动

# ./nginx //先进入/usr/local/nginx/sbin目录下

进入启动目录直接运行nginx即可启动Nginx服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,请参考2.1.1。需要注意的是,若服务器中已安装有httpd等其他WEB服务软件,应采取措施避免部突。

2.2、停止

#./nginx -s quit(推荐)

#ps -ef|grep nginx(查出进程PID)
#kill -QUIT 80(杀死进程)

#./nginx -s stop

2.3、重启

#cd /usr/local/nginx/sbin
#./nginx -s reload

3、配置防火墙开放端口

#修改防火墙配置:

# vi /etc/sysconfig/iptables

#添加配置项

-AINPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 #重启防火墙

# service iptables restart

# /etc/init.d/network restart(重启网络配置,一般不需要)

这样nginx的web服务就可以通过80端口访问了

4、配置开机自启动Nginx

这里使用的是编写shell脚本的方式来处理

vi /etc/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=/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

:wq  保存并退出

设置文件的访问权限:

# chmod a+x /etc/init.d/nginx   //a+x ==> all user can execute 所有用户可执行
// chmod 555/etc/init.d/nginx

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx.

同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

# vi /etc/rc.local
 /etc/init.d/nginx start //加入此行

保存并退出,下次重启会生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值