我虚拟了三台centos7,要将one(192.168.56.31)、two(192.168.56.32)配置成nginx+keepalived双机热备(思路是当一台服务器的nginx挂掉时,可以自动用另一台的nginx,就是当主服务器nginx的端口不通时,自动关闭自己的keepalived服务,这样虚拟ip就会指到备用ip上),three(192.168.56.33)做tomcat服务器。实验环境下先把防火墙和selinux关闭:setenforce 0 && systemctl stop firewalld
首先进行nginx的安装,我用的是最简单的yum安装方式,自动的yum源中没有nginx,我们需要安装一下yum源然后再来安装nginx:yum -y install epel-release && yum -y install nginx
。然后再来安装一下keepalived:yum -y install keepalived
。安装完成后可以使用keepalived --help
来进行keepalived的命令帮助。两者的配置文件都在etc目录下。再将jdk和tomcat上传到three中并安装,我这设置了三个tomcat实例,端口分别为:8180、8280、8380。
然后先来配置nginx,配置文件在/etc/nginx/中:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx; #运行用户,避免权限问题,就用root吧
worker_processes auto; #工作进程数,表示自动选择,cpu多的话,可以手动设置一般为cpu的倍数。
error_log /var/log/nginx/error.log; #错误日志路径
pid /run/nginx.pid; #PID文件路径
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; #包含这个文件的信息
events {
worker_connections 1024; #工作连接数,指一个进程可以产生多少个连接,用 ulimit -n 来查看,但是这是理论上,没必要设置这么大
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #定义访问日志的显示格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #访问日志的路径
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types; #将指定的配置文件引入到这个文件中来
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/