yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
nginx安装
[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
省略安装内容...
[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install
省略安装内容...
3、启动Nginx
先找一下nginx安装到什么位置上了
进入nginx目录并启动
nginx的基本操作
启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
验证配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
负载均衡监听地址:
http://10.88.202.19:8888/
http://10.88.202.20:8888/
http://10.88.202.21:8888/
tomcat服务地址:
http://10.88.202.19:8880/Demo/index.html
http://10.88.202.20:8880/Demo/index.html
http://10.88.202.21:8880/Demo/index.html
总入口:
http://10.88.202.19:8888【监听端口】/Demo
windows电脑,本地改成映射的ip域名:
总入口:
http://wcq20:8888/Demo
nginx配置限制同一个ip的访问频率
1、在nginx.conf里的http{}里加上如下代码:
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
2、在需要限制并发数和下载带宽的网站配置server{}里加上如下代码:
limit_conn perip 2;
limit_conn perserver 20;
limit_rate 100k;
补充说明下参数:
$binary_remote_addr是限制同一客户端ip地址;
$server_name是限制同一server最大并发数;
limit_conn为限制并发连接数;
limit_rate为限制下载速度;
修改linux主机名
https://jingyan.baidu.com/article/574c52192a1d8d6c8d9dc1ce.html
2、更改/etc/sysconfig下的network文件,在提示符下输入vi /etc/sysconfig/network,然后将HOSTNAME后面的值改为想要设置的主机名。
3、更改/etc下的hosts文件,在提示符下输入vi /etc/hosts,然后将localhost.localdomain改为想要设置的主机名。
4、在提示符下输入reboot命令,重新启动服务器。
5、重启完成后用hostname命令查询系统主机名,可以看出系统主机名已经变更为mycomputer。
keepalived安装:
https://www.cnblogs.com/skying555/p/8024503.html
安装依赖
yum -y install gcc gcc+ gcc-c++
yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel
安装内核
yum -y install kernel kernel-devel
当前kernel代码建立连接 ln -s /usr/src/kerners/2.6....../ /usr/src/linux
安装keepalived
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure
make
make install
拷贝相应的文件
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -r /usr/local/etc/keepalived/ /etc/
配置keeplived和nginx主机
进入keepalived目录,上传脚本
#!/bin/sh
ng_thread=$(ps -ef|grep nginx |grep process|wc -l)
Tom_thread=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}')
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')],进程ID为:&Tom_thread"
if [ $ng_thread -eq 0 ];then
echo "Nginx已经停止,启动Nginx,Starting nginx!"
/usr/local/nginx/sbin/nginx
elif [ ! $Tom_thread ];then
echo "8880的Tomcat已经停止,启动!"
sh /opt/tomcat8880/bin/startup.sh
fi
cd /etc/keepalived
上传服务器后,文件需要获取执行权限
chmod +x /etc/keepalived/nginx_check.sh
两个服务器都执行以下操作:
vi /etc/keepalived/keepalived.conf
Tips:
state 参数值:主的是MASTER、备用的是BACKUP
priority 参数值: MASTER > BACKUP
virtual_router_id: 参数值要一样
测试测试:
两台测试机134\135均启动keepalived和nginx
service keepalived restart
service keepalived nginx
验证nginx启动正常:
ip a 验证虚拟IP是否成功
访问 master:http://10.88.202.19:8888/
访问 backup: http://10.88.202.20:8888/
查看keepalived的日志信息:
134\135均打开日志信息方便查看keepalived动态:
tail -f /var/log/messages
浏览器打开虚拟ip访问:http://192.168.232.16:8080/ ,此时显示IP为10.88.202.19
http://10.88.202.88:8888/
http://10.88.202.88:8888/Demo
常用操作命令
vim /etc/keepalived/keepalived.conf
service keepalived restart
vim /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload [stop start]
其他
Linux测试Nginx
elinks 10.88.202.20:8888 --dump
测试主从keepalived