阿里云服务器部署 五 Nginx + springboot

Nginx的部分配置

1. 基础容灾配置(被动健康检查)

upstream 块中,通过 max_failsfail_timeout 参数定义故障转移规则:

upstream 块中,通过 max_failsfail_timeout 参数定义故障转移规则:

nginx

复制

复制代码
upstream backend {
    # 定义后端服务器,设置失败阈值和超时
    server 172.16.108.42:80 max_fails=3 fail_timeout=10s;  # 10秒内失败3次则标记为不可用
    server 172.16.108.43:80 max_fails=3 fail_timeout=10s;
    
    # 负载均衡策略(可选)
    least_conn;  # 最少连接数策略
}
参数说明:
  • max_fails:在 fail_timeout 时间内,允许的最大失败请求次数。

  • fail_timeout:服务器被标记为不可用的时间(超时后自动恢复探测)。

1. 权重(weight)的作用

  • 默认值 :如果未指定 weight,默认值为 1

  • 流量分配规则:根据权重值的比例分配请求。

  • 适用场景:后端服务器性能不均衡时(如一台性能强、一台性能弱),通过权重调整流量分配。

效果:
  • 当某台服务器连续失败 3 次后,Nginx 会将其标记为不可用,10秒内不再分配请求

  • 10秒后,Nginx 会尝试重新发送请求探测是否恢复。

复制代码
http {
    # ... 其他原有配置 ...

    upstream ai-backend {
        server 172.16.108.42:10011 weight=1 max_fails=3 fail_timeout=10s;
        server 172.16.108.43:10011 weight=1 max_fails=3 fail_timeout=10s;
        keepalive 32;
        least_conn;
    }

    server {
        listen 10011;
        server_name 172.16.108.41;

        location / {
            proxy_pass http://ai-backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
            # 超时时间设为20分钟
            proxy_connect_timeout 1200s;
            proxy_read_timeout     1200s;
            proxy_send_timeout     1200s;
        }
    }

    # ... 其他服务配置 ...
}

http {
    # ... 原有其他配置(如AI服务、日志格式等) ...

    # 定义词向量服务的upstream
    upstream wordvec-backend {
        server 172.16.108.44:10011 weight=1 max_fails=3 fail_timeout=10s;  # 本地Nginx服务器的10011端口(若服务部署在Nginx本机)
        server 172.16.108.42:10011 weight=1 max_fails=3 fail_timeout=10s;  # 另一台服务器的10011端口
        keepalive 32;                # 保持长连接
        least_conn;                  # 最少连接数负载均衡
    }

    # 词向量服务的独立监听端口(例如10012)
    server {
        listen 6001;                # 监听外部请求的端口
        server_name 172.16.108.41;   # Nginx服务器IP或域名

        location / {
            proxy_pass http://wordvec-backend;  # 转发到词向量后端
            proxy_http_version 1.1;
            proxy_set_header Connection "";    # 启用HTTP 1.1长连接
            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_set_header X-Forwarded-Proto $scheme;

            # 超时时间设为20分钟(1200秒)
            proxy_connect_timeout 1200s;  # 连接后端超时
            proxy_read_timeout     1200s;  # 读取响应超时
            proxy_send_timeout     1200s;  # 发送请求超时
        }
    }
}

1. 检查配置文件语法

在重启前 必须验证配置正确性,避免错误配置导致服务崩溃:

sudo nginx -t

nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:57

nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:64

nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:71

nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:79

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

2. 重新加载配置(平滑重启)

传统方式:

复制代码
sudo nginx -s reload

[root@bigdata41 nginx]# sudo nginx -s reload
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:57
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:64
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:71
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:79
nginx: [error] invalid PID number "" in "/run/nginx.pid"

报异常 暂未处理

3. 完全重启服务(强制重启)
复制代码
sudo systemctl restart nginx

sudo systemctl restart nginx 重新启动nginx

sudo systemctl status nginx 查看nginx 状态

将41上已有的6001端口服务迁移走

sudo systemctl status nginx

相关推荐
TG:@yunlaoda360 云老大5 分钟前
阿里云国际站GPU:怎么通过通过VNC连接实例?
服务器·阿里云·云计算
liulilittle2 小时前
LwIP协议栈MPA多进程架构
服务器·开发语言·网络·c++·架构·lwip·通信
观山岳五楼2 小时前
unbuntu系统配置IPV6的三种模式
linux·服务器·ip·1024程序员节
运维行者_2 小时前
AWS云服务故障复盘——从故障中汲取的 IT 运维经验
大数据·linux·运维·服务器·人工智能·云计算·aws
青靴3 小时前
关于NGINX,你了解多少?
运维·nginx
无聊的小坏坏3 小时前
从零开始:C++ 多进程 TCP 服务器实战(续篇)
服务器·c++·tcp/ip
命运之光3 小时前
【快速解决】Linux服务器安装Java17运行环境
linux·运维·服务器
你喜欢喝可乐吗?3 小时前
Ubuntu服务器无法显示命令行登录提示
linux·运维·服务器·ubuntu
国科安芯4 小时前
AS32S601ZIT2抗辐照MCU在商业卫星飞轮系统中的可靠性分析
服务器·网络·人工智能·单片机·嵌入式硬件·fpga开发·1024程序员节
FJW0208144 小时前
【Linux】用户管理及优化
linux·运维·服务器