UBUNTU中nginx反向代理(负载均衡)

现有三台虚拟机(ubuntu、centos7、centos8)

centos 7安装apache(192.168.100.88:80)

yum install -y httpd

启动服务并设置开机自启

systemctl start httpd

systemctl enable httpd

关闭防火墙、关闭沙盒

systemctl stop firewalld

setenforce 0

验证是否运行成功

systemctl status httpd

页面验证

centos8 安装 Apache (192.168.100.91:80)

dnf install -y httpd

启动

httpd systemctl start httpd

设置开机自启

systemctl enable httpd

查看运行状态

systemctl status httpd

关闭防火墙、关闭沙盒

systemctl stop firewalld

setenforce 0

验证

ubuntu安装nginx(192.168.100.99:80)

更新系统软件源

apt update

apt upgrade -y

安装 Nginx

apt install nginx -y

检查 Nginx 是否运行

systemctl status nginx

放行防火墙(或者关闭ufw)

ufw allow 'Nginx Full'

配置代理负载文件(若是其他的系统,寻找相对应的本地配置文件修改)

vim /etc/nginx/sites-available/default

upstream backend_servers {

负载均衡策略

server 192.168.100.88:80; # 后端服务1

server 192.168.100.91:80; # 后端服务2

ip_hash; # IP 绑定(会话保持)

least_conn; # 最少连接

weight 轮询示例

server 127.0.0.1:8080 weight=5;

server 127.0.0.1:8081 weight=3;

}

server {

listen 80;

server_name _;

location / {

proxy_pass http://backend_servers;

传递真实IP

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

超时

proxy_connect_timeout 5s;

proxy_read_timeout 10s;

proxy_send_timeout 10s;

}

}

:wq 保存退出

重启服务

systemctl restart nginx

查看服务

相关推荐
小猿M1 天前
在Ubuntu中安装CRIU
ubuntu·criu
锋行天下1 天前
让nginx网关扛下所有攻击
前端·后端·nginx
农民小飞侠1 天前
SandboxFusion搭建教程
linux·ubuntu
代码熬夜敲Q2 天前
Nginx相关
运维·服务器·nginx
shandianchengzi2 天前
【记录】Ubuntu26|通过网页和ydotool用手机远程输入文本到电脑上,方便接入手机上优越的语音输入法
ubuntu·手机·工具·输入·软件·输入法
phltxy2 天前
HAProxy安装与RabbitMQ负载均衡配置
分布式·rabbitmq·负载均衡
shandianchengzi2 天前
【记录】Claude Code|Ubuntu26给Claude Code新增任务消息提示音
运维·服务器·ubuntu·ai·大模型·音频·claude
念何架构之路2 天前
接入层Nginx
运维·nginx
大明者省2 天前
Ubuntu Python 部署终极版教程
开发语言·python·ubuntu