实验环境:
centos6.9搭建:统一关闭selinux、iptables保证网络的连通性,配置主机名,统一hosts文件,搭建yum仓库。
nginx1 192.168.200.101
keepalived VIP 192.168.200.100
nginx2 192.168.200.102
tomcat1 192.168.200.103
tomcat2 192.168.200.104
mysql 192.168.200.105
nginx调度搭建(101)
http://nginx.org/
useradd -M -s /sbin/nologin nginx 创建nginx账户,为了安全禁止登陆系统
yum -y install pcre-devel zlib-devel openssl-devel 安装依赖包(如果安装报错,按报错信息安装安装所需要的依赖包)
tar xf nginx-1.14.0.tar.gz -C /usr/src/ 解压nginx包
cd /usr/src/nginx-1.14.0/ 进入解压目录
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_flv_module --with-http_stub_statu
s_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module && make && make install 配置编译安装
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 将nginx自带的启动脚本链接到可以被环境变量检测到
cd /usr/local/nginx/conf/ 进入nignx配置目录
cp nginx.conf{,.bak} 将nginx配置文件备份
vim nginx.conf 修改nginx配置文件
user nginx nginx; 全局配置 指定nginx用户和用户组
worker_processes 2; 指定nginx的进程数,每个进程消耗10M-20M内存之间,一般建议与CUP核数相同
error_log logs/error.log; 错误日志
pid logs/nginx.pid; nginxPID文件位置
events { I/O事件配置
use epoll; 使用epoll(linux2.6的高性能方式)
worker_connections 10240; 每个进程最大连接数(最大连接=连接数×进程数)(调整进程最大打开文件连接数:ulimit -u 65535)
}
http { HTTP配置及虚拟主机设置
include mime.types; 指定配置文件所包含的文件
default_type application/octet-stream; 指定默认类型为二进制流
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 设置日志格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_tokens off; 隐藏版本号
access_log logs/access.log main; 指定日志文件的路径及使用那种日志格式记录日志
add_header X-Server $hostname;
server_names_hash_bucket_size 128; 保存服务器名字的hash表大小
server_name_in_redirect off; 以当前服务器的IP地址进行拼接URL(URL重定向)
sendfile on; 启动高效传输文件的模式
tcp_nopush on; 允许在Linux和FreeBSD 4.*上将响应头和正文的开始部分一起发送,一次性发送整个文件。
tcp_nodelay on; 降低网络里小包的数量,从而提升网络性能。
keepalive_timeout 60; 连接保持超时时间
client_header_buffer_size 32k; 上传文件大小限制
large_client_header_buffers 4 128k; 设定请求缓
client_max_body_size 512m; 设定请求缓
open_file_cache max=65535 inactive=20s; 文件信息进行缓存:
open_file_cache_valid 30s; 30s后会检查此文件的更改信息是否变化,发现变化就更新
open_file_cache_min_uses 1;
gzip on; 开启 gzip 压缩输出
gzip_static on;
gzip_http_version 1.1; 设置识别 http 协议版本,默认是 1.1
gzip_comp_level 2; 设置压缩比例,等级1-9
gzip_min_length 1024; 用于设置允许压缩的页面最小字节数
gzip_vary on; 选项可以让前端的缓存服务器经过 gzip 压缩的页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss; 指定压缩类型
fastcgi_connect_timeout 300; fastcgi 进程管理器连接超时时间
fastcgi_send_timeout 300; 发送超时时间
fastcgi_read_timeout 300; 读取超时时间
fastcgi_buffer_size 512k; 缓冲超时时间
fastcgi_buffers 6512k; 最大缓冲
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 54k;
proxy_temp_file_write_size 2m;
proxy_ignore_client_abort on;
proxy_cache_path /usr/local/nginx/cache_temp levels=2:2 keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;
}
mkdir /usr/local/nginx/conf/conf.d
vim /usr/local/nginx/conf/conf.d/server.conf
server {
listen 80;
server_name www.bonana.com 192.168.200.101;
index index.html index.htm index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.bonana.com_access.log main;
location ~ {
index index.jsp;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_servers;
}
location ~ .*\.(gif|jpg|jpeg|bmp|swf|png)$ {
expires 30d;
}
location ~ .*\.(js|css)$ {
expires 1h;
}
}
vim /usr/local/nginx/conf/conf.d/pool.conf
upstream tomcat_servers {
ip_hash; (一般不适用生成环境)
server 192.168.200.103:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.200.104:8080 weight=1 max_fails=3 fail_timeout=10s;
}
nginx -t 检测nginx配置文件是否正确
nginx 启动nginx服务
killall -1 nginx killall -s HUP nginx 平滑重启
killall -3 nginx 正常停止
killall -s USR1 nginx nginx日志切换
keepalived部署
http://www.keepalived.org/download.html
yum -y popt-devel kernel-devel openssl-devel
./configure --prefix=/ --with-kerneldir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
cd /etc/keepalived/
cp -p keepalived.conf keepalived.conf.bak
vim keepalived.conf
notification_email {
xiangjiao_bonana@163.com
}
notification_email_from xaingjiao_bonana@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_1
}
vrrp_script nginx {
script /opt/chk_nginx.sh
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.100
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
nopreempt 非抢占模式
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.200
}
}
vim /opt/chk_nginx.sh 编辑监控nginx存活脚本
#!/bin/bash
# check nginx server status
NGINX="/usr/local/nginx/sbin/nginx"
nginxpid=$(ps -C nginx --no-header |wc -l)
if [ $nginxpid -eq 0 ];then
$NGINX
sleep 3
nginxpid=$(ps -C nginx --no-header |wc -l)
if [ $nginxpid -eq 0 ];then
/etc/init.d/keepalived stop
echo "Keepalived stopped ,please check your Nginx !"|tee -a /var/log/messages
fi
fi
chmod +x /opt/chk_nginx.sh
/etc/init.d/keepalived start
nginx2 的配置 (102)
vim /usr/local/nginx/conf/conf.d/server.conf
server {
listen 80;
server_name www.bonana 192.168.200.102;
index index.html index.htm index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.bonana.com_access.log main;
location ~ .*\.jsp$ {
index index.jsp;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_servers; }
location ~ .*\.(gif|jpg|jpeg|bmp|swf|png)$ {
expires 30d;
}
location ~ .*\.(js|css)$ {
expires 1h;
}
}
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
crushlinux@163.com
}
notification_email_from crushlinux@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_2
}
vrrp_script nginx {
script /opt/chk_nginx.sh
interval 2
weight -10
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99 advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.100
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.200.200
}
}
scp 192.168.200.101:/opt/chk_nginx.sh /opt
/etc/init.d/keepalived start
高可用测试 nginx1(101)
ip addr show eth0 查看VIP
/etc/init.d/keepalived stop (停止101,查看102VIP)
tomcat1 的安装配置
rpm -aq | grep jdk
rpm -e java-1.7.0-openjdk-1.7.0.131-2.6.9.0.el6_8.x86_64 --nodeps