NGINX反向代理和负载均衡的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31642819/article/details/81511021

1.nginx的安装与部署

     1.1 http://nginx.org/en/download.html下载最新版nginx

           解压tar -zxvf nginx-x.x.x.tar.gz

     1.2安装相关依赖

         1.ssl 功能需要 openssl 库 使用 yum -y install openssl 安装

         2.gzip 模块需要 zlib 库 使用 yum -y install zlib 安装

         3.rewrite 模块需要 pcre 库 使用 yum -y install pcre 安装

                如果没有安装c++编译环境,可通过yum install gcc-c++安装

     1.3创建nginx 运行使用的用户

          groupadd www

          useradd -g www www

     1.4编译         

./configure --prefix=/opt/nginx/nginx-x.x.x \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-http_realip_module

    1.5安装

make && make install
ln -s /opt/nginx/nginx-x.x.x /usr/local/nginx

    1.6如果需要加入系统服务,通过/etc/init.d/nginx命令启动及查看nginx 状态,可在/etc/init.d/目录下新建编辑nginx文件,加入以下内容:

#!/bin/sh
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server. It is used to serve
# Source function library.
. /etc/rc.d/init.d/functions
  
# Source networking configuration.
. /etc/sysconfig/network
  
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
  
# 这里要根据实际情况修改
nginx="/usr/local/nginx"
prog=$(basename $nginx)
  
# 这里要根据实际情况修改
NGINX_CONF_FILE="/data/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
  
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    killall -9 nginx
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)   
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

修改权限

chmod 755 /etc/init.d/nginx

添加系统服务:

chkconfig --add nginx

可通过 service nginx status|start|stop|reload 查看nginx状态

2.nginx反向代理

      2.1在nginx的安装目录找到nginx.conf配置文件,添加如下配置:

upstream toproxy {
            server 11.1.11.11:8080;
            server 11.1.11.11:8081;
            server 11.1.11.11:8082;
        }
        server {
            listen 10.0.00.00:8080;
            location / {
                proxy_pass http://toproxy;
                }
        }

然后启动nginx,由于配置了proxy_pass地址,所有请求都会先通过nginx反向代理服务器,在服务器将请求转发给upstream,所

以访问http://10.0.00.00:8080就会将请求转发到http://11.1.11.11:8080;http://11.1.11.11:8081;http://11.1.11.11:8082

     当然,如果访问被拒绝,有可能是服务器的防火墙没有关闭;

     关闭防火墙:sudo systemctl stop firewalld.service

2.Nginx实现负载均衡

     2.1nginx负载均衡分发策略

      Nginx 的 upstream目前支持的分配算法: 
          2.1.1轮询 ——1:1 轮流处理请求(默认)

 每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继续轮询。 
          2.1.2权重 ——you can you up
 通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况,weight和访问比率成正比。 
          2.1.3ip_哈希算法
 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。 

猜你喜欢

转载自blog.csdn.net/qq_31642819/article/details/81511021