nginx安装和配置负载均衡

一.nginx安装

步骤1:
yum install gcc-c++ -y
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

步骤2:解压nginx-1.8.0.tar.gz文件
tar -zxvf nginx-1.8.0.tar.gz

步骤3:配置nginx参数
注意:下边将临时文件目录指定为/var/temp/nginx,需要在/var 下创建temp及nginx目录
cd /var
mkdir temp
cd temp
mkdir nginx
---------------------
cd nginx-1.8.0
参数设置如下:
./configure
–prefix=/usr/local/nginx
–pid-path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/scgi

步骤4:编译和安装
cd nginx-1.8.0
make
make install
安装成功后查看目录
cd …/nginx
ll

步骤5:启动nginx、停止nginx
–启动nginx----------------
cd /usr/local/nginx/sbin/
./nginx
ps aux|grep nginx

–指定nginx启动配置文件------------------
./nginx -c /usr/local/nginx/conf/nginx.conf

–停止nginx----------------
cd /usr/local/nginx/sbin
./nginx -s stop

–nginx完整停止----------------
cd /usr/local/nginx/sbin
./nginx -s quit

步骤6:重启nginx
cd /usr/local/nginx/sbin/
./nginx -s reload

步骤7:开机自启动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

-----设置文件的访问权限----------------
chmod a+x /etc/init.d/nginx

说明:
(a+x ==> all user can execute 所用户可执行):

–加入到rc.local文件中----------------
vi /etc/rc.local
末尾加入:/etc/init.d/nginx start

–查看nginx状态、启动、停止、重启----------------
/etc/init.d/nginx status
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart

二.nginx负载均衡配置

  1. 修改nginx/conf/nginx.conf 配置如下
    1 upstream 配置负载均衡
    2 server nginx 代理的服务器
    3 weight:权重
    4 proxy_pass:访问nginx 的地址名称
    2.nginx.conf配置如下:
    upstream www.ego.com{
    server 192.168.126.146:1080 weight=2;
    server 192.168.126.146:2080 weight=1;
    server 192.168.126.146:3080 weight=1;
    server 192.168.126.146:4080 weight=1;
    }
    server{
    listem 80;
    server_name www.ego.com;
    location /{
    proxy_pass http://www.ego.com;
    }

    }

你可能感兴趣的:(linux)