nginx keepalived,基于ubuntu系统上虚拟ip怎么配置
时间: 2024-05-11 09:13:56 浏览: 89
Nginx配置基于多域名、端口、IP的虚拟主机
以下是在Ubuntu系统上配置nginx和keepalived基于虚拟IP的步骤:
1. 安装nginx和keepalived:
```
sudo apt update
sudo apt install nginx keepalived
```
2. 配置nginx
在配置nginx之前,需要确保nginx已经正常启动并监听在正确的端口。打开默认的nginx配置文件:
```
sudo nano /etc/nginx/sites-available/default
```
将`server_name`设置为虚拟IP地址,例如:
```
server {
listen 80;
server_name 192.168.1.100; # 虚拟IP地址
...
}
```
保存并退出文件。
3. 配置keepalived
打开keepalived配置文件:
```
sudo nano /etc/keepalived/keepalived.conf
```
添加以下配置:
```
vrrp_script chk_nginx {
script "killall -0 nginx" # 检查nginx是否运行
interval 2 # 每2秒检查一次
}
vrrp_instance VI_1 {
interface eth0 # 监听的网卡
state MASTER # 主节点为MASTER,备份节点为BACKUP
virtual_router_id 51 # 路由ID,要和备份节点一致
priority 101 # 主节点优先级,要比备份节点高
advert_int 1 # 每秒向其他节点发送VRRP通告
authentication {
auth_type PASS # 认证类型为密码
auth_pass 1111 # VRRP密码
}
virtual_ipaddress {
192.168.1.100/24 dev eth0 # 虚拟IP地址和掩码
}
track_script {
chk_nginx # 跟踪nginx是否运行,如果nginx停止运行,则切换到备份节点
}
}
```
保存并退出文件。
4. 启动keepalived
启动keepalived服务:
```
sudo systemctl start keepalived
```
检查服务状态:
```
sudo systemctl status keepalived
```
如果输出显示服务已经启动并正在运行,则说明配置成功。
以上就是在Ubuntu系统上配置nginx和keepalived基于虚拟IP的步骤。
阅读全文