【RedHat Linux】Nginx 配置反向代理与负载均衡

【nginx配置】

《安装》
yum install epel-release -y
yum install nginx -y 安装nginx
yum -y install net-tools//下载net工具包
yum update && yum update//更新系统
yum -y install links//下载命令行web浏览器
yum -y install httpd//下载http服务器

《测试nginx》
systemctl start nginx //启动nginx
ifconfig //查看本机ip
links 192.168.159.130//测试Nginx启动是否正常

《自定义网页根目录》
vi /etc/nginx/nginx.conf //修改配置文件
root /var/www/html; // log / {} 内修改
index index.html; //没分号必死!!!
mkdir -p /var/www/html //创建自定义目录
touch /var/www/html/index.html

echo hello web >> /var/www/html/index.html
systemctl resload nginx 重新加载Nginx
links 192.168.159.130//测试效果
# cat /var/log/nginx/error.log //如果出错,查看错误日志并修改
# 如果出现403,检查:index权限是否有x,selinux是否已经关闭

《配置反向代理》
yum install rpcbind nfs-utils -y //安装rpcbind,下个实验使用,提前安装
systemctl enable rpcbind,service //开机启动
systemctl enable nfs-server.service
systemctl enable nginx
shutdown now //不shutdown无法克隆
#克隆成4台,分别是3台服务器,1台负载均衡(同时作存储)

服务器:
echo "im 1" > /var/www/html/index.html //3个服务器写入不同内容

代理:
vi /etc/nginx/nginx.conf
http {}内添加:
upstream test130 { //分配组
#ip_hash; //不写默认轮询,最小连接调度为:least_conn;
server 192.168.159.131; // 已启用DHCP,各服务器IP地址用ifconfig查询
server 192.168.159.132; //
server 192.168.159.133; //

location / { proxy_pass http://test130; } //代理分配给test130组
:wq
systemctl reload nginx
links 192.168.159.130 //正常连通后,刷新3次看是否网页显示不一样

《配置nfs》
存储:
#添加一个新硬盘,作网络共享盘
mkfs.xfs /dev/sdb
mkdir /share
chomd -R o+w /share //改权限
mount /dev/sdb /share
echo "/share 192.168.31.0/24(rw,sync,fsid=0)" > /etc/exports
#showmount -e 查看共享的服务
#showmount -a 查看挂载情况
服务器:
getenforce //查看selinux
setenforce 0 //关闭selinux
echo "/share 192.168.159.0/24(rw,sync,fsid=0)" >> /etc/exports

《相关》
systemctl restart nginx
systemctl stop firewalld 关闭防火墙
systemctl disable firewalld 永久关闭防火墙
systemctl enable/disable 。。。 设置/关闭开机启动
iptables -F 修改防火墙规则(-F为全清)
tail -f /var/log/nginx/access.log 访问日志
#cd /etc/yum.repos.d yum安装目录
#ls 查看epel是否安装成功
tail -f /var/log

《nginx.conf解析》
vim /etc/nginx/nginx.conf
设置格式代码:
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
远程地址 - 远程用户名 时间 请求
'$status $body_bytes_sent "$http_referer" '
访问状态 文件大小 请求头
'"$http_user_agent" "$http_x_forwarded_for"'

//referer 标识连接的来源,可用于统计连接,也可以用于防盗链

access_log /var/log/nginx/access.log main //此main对应上面main可替换


//serve{}内为全局变量
//location{}为临时变量 优先
root 路径; 默认路径
index 文件1 文件2; 默认文件 空格隔开
index index.html 正常文件

【正向代理、反向代理】
正常代理:知道目标服务器,代理给牵线
反向代理:知道服务,代理给分配服务器

《负载均衡load balancer》
round-robin 轮询 轮着分配服务器,默认
least-connented 最小连接调度 少连接的分配
ip-hash hash基于客户的ip进行分配,回话保持



【网络存储的概念】
多台服务器挂载到一台共享存储,实现数据互通
nfs 网络文件系统,提供远程共享服务
服务器——远程过程调用RPC——存储
文件存储:直接读写执行
对象存储:必须通过uri下载修改完重传才能改变对象内容

《支持模块》
yum install rpcbind nfs-utils -y //安装rpcbind功能

《修改配置文件》
vi /etc/exports
<输出目录>[客户端1 选项(访问权限,用户映射,其他)][客户端2 ...]
/share 192.168.31.0/24(rw,sync,fsid=0)
网络号

rpm -qa |grep rpcbind 检查是否安装bind
rpm -qa |grep nfs-utils

《存储设置》
mkdir /share //创建目录
chomd -R o+w /share //改权限
echo "/var/share 192.168.159.0/24(rw,sync,fsid=0)" >> /etc/exports
systemctl enable rpcbind,service 开机启动
systemctl start rpcbind.service 启动服务 bind先启动
systemctl start nfs-server.service

#systemctl status rpcbind,service 检查是否启动
#rpcinfo 检查NFS服务器是否启动成功
#exports
#showmount -e 查看共享的服务

《服务器设置》
showmount -e 192.168.159.130 //查看存储共享情况
mount -t nfs 192.168.159.130:/share /var/www/html/ 挂载网盘
setenforce 0 //关闭selinux
getenforce //查询selinux

【作死源代码安装Nginx】
//必须的软件库包括:gcc(gcc-* glibc-*),openssl(openssl openssl-devel),
//zlib(zlib zlib-devel),pcre(pcre pcre-devel)
yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
//如果已经 yum groupinstall 'development tools' 'Development Libraries'-y 上步可省
ls
mkdir /usr/local/nginx
wget https://nginx.org/download/nginx-1.12.1.tar.gz
tar xvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
ls
./configure //查看结果进行配置

//默认地址是/usr/local/nginx/nginx
./configure \
> --sbin-path=/usr/local/nginx \
> --conf-path=/usr/local/nginx/nginx.conf \
> --pid-path=/usr/local/nginx/nginx.pid \
> --with-http_ssl_module

make
make install

PS:源代码安装是rpm、yum是看不到的,可用find / -name nginx,或者自己翻找安装目录看看有没文件
posted @ 2018-07-30 12:09  caya  阅读(398)  评论(0编辑  收藏  举报