依赖环境

yum install -y wget  
yum install -y vim-enhanced  
yum install -y make cmake gcc gcc-c++  
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

 

下载nginx-1.12.2.tar.gz

wget http://nginx.org/download/nginx-1.12.2.tar.gz

 

编译安装
解压:
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2

执行如下命令

./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 \
--with-http_stub_status_module \
--with-http_ssl_module \
--http-scgi-temp-path=/var/temp/nginx/scgi

 

上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp/nginx目录

sudo mkdir -p /var/temp/nginx

开始编译和安装

make 
make install

 

启动Nginx

启动

cd /usr/local/nginx/sbin/
./nginx

 

查看

ps -aux | grep nginx

 

打开浏览器 http:// +linux的ip:80 出现如下图表示nginx安装成功并成功访问了

这里写图片描述

退出Nginx

cd /usr/local/nginx/sbin
./nginx -s quit
  • 1
  • 2

重启Nginx

cd /usr/local/nginx/sbin
./nginx -s reload
  • 1
  • 2
  • 3

反向代理负载均衡配置

配置
以vim模式打开nginx.conf配置文件

cd /usr/local/nginx/conf/
vi nginx.conf
  • 1
  • 2

根据上边的需求在nginx.conf文件中配置负载均衡,如下:

在server上添加此upstream节点

upstream mytomcat{
    #分权 即访问131与134的次数比例为1比1
        server 需要代理的ip1 weight=1;
        server 需要代理的ip2 weight=1;
        }

    server {
        listen 80;
        server_name localhost;
        #即所有请求都到这里去找分配
        location / {
       #使用mytomcat分配规则,即刚自定义添加的upstream节点
           proxy_pass http://mytomcat;
        }
    }