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;
    }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java之书

会持续更新实用好的文章谢谢关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值