守护网站心脏:HAProxy负载均衡技术详解(全网最详细)

news/2024/9/11 4:17:23/ 标签: 负载均衡, 运维, 服务器, linux

守护网站心脏:HAProxy负载均衡技术详解

文章目录

  • **守护网站心脏:HAProxy负载均衡技术详解**
    • haproxy介绍
      • 特点
      • 功能
      • 配置
      • 结论
    • haproxy负载均衡测试
      • haproxy主机操作
        • 日志重定向操作
      • webserver1主机操作
      • webserver2主机操作
    • haporxy相关配置测试
      • backup参数测速
        • haproxy主机操作
        • webserver1主机操作
        • webserver2主机操作
      • disabled参数测速
        • haproxy主机操作
        • webserver1主机操作
        • webserver2主机操作
      • redirect prefix 参数测试
        • haproxy主机操作
        • 浏览器访问测试
    • haporxy的热处理,注意:重启后会失效
      • haproxy多进程如何热处理
    • haproxy调度算法详解
      • Round Robin (轮询)
        • 描述
        • 配置示例
        • 适用场景
        • 注意事项
      • Least Connections (最少连接数)
        • 描述
        • 配置示例
        • 适用场景
        • 注意事项
      • Source (源地址哈希)
        • 描述
        • 配置示例
        • 适用场景
        • 注意事项
      • URI (URI 哈希)
        • 描述
        • 配置示例
        • 适用场景
        • 注意事项
        • 总结
    • haproxy状态页
      • 浏览器测试
    • haproxy的cookie会话保持
      • haproxy主机操作
      • cookie添加参数解释
    • haproxy的IP透传
      • 七层透传(mode=http)nginx
        • haproxy主机操作
        • webserver2主机(nginx)操作
      • 七层透传(mode=http)apache
        • haproxy主机操作
        • webserver1主机(apache)操作
      • 四层透传(mode=tcp)nginx
        • haproxy主机操作
        • webserver2主机(nginx)操作
      • 四层透传(mode=tcp)apache
      • 总结
        • client主机操作
    • haproxy的acl基本访问控制
      • haproxy主机操作
      • client主机测试
    • haproxy的acl一些常用限制
      • 基于域名的访问控制
        • haproxy主机操作
        • client主机操作
      • 基于IP的访问控制
        • haproxy主机操作
        • client主机操作
        • haproxy主机测试
      • 基于IP的访问控制之黑名单
        • haproxy主机操作
        • client主机操作
        • haproxy主机测试
        • 在这个实验中172.25.254.200 -- client客户端,172.25.254.20 -- webserver2主机端
      • 基于浏览器的访问控制
        • haproxy主机操作
        • client主机操作
        • 浏览器访问测试
      • 基于文件后缀名来实现动静分离
        • haproxy主机操作
        • webserver1主机操作
        • client主机操作
        • 浏览器访问控制
      • 基于网址后面的目录文件的访问控制
        • haproxy主机操作
        • webserver1主机操作
        • webserver2主机操作
        • 浏览器访问测试
    • haproxy自定义错误页面
      • haproxy主机操作
      • webserver1主机操作
      • webserver2主机操作
      • client主机操作
      • 浏览器访问测试
    • haproxy报错后跳转指定网站
      • haproxy主机操作
      • 浏览器测试
    • haproxy四层负载数据库
      • haproxy主机操作
      • webserver1主机操作
      • webserver2主机操作
      • client主机操作
    • haproxy之https网站加密
      • haproxy主机操作
      • webserver1主机操作
      • webserver2主机操作
      • 浏览器访问测试
    • haproxy之https网站强制全站加密
      • haproxy主机操作
      • 浏览器访问测试

haproxy介绍

HAProxy(High Availability Proxy)是一个免费且开源的软件,被广泛用于实现高性能、高可用性的负载均衡和代理服务。它最初由 HA-SOFTWARE 开发,并已成为许多企业和组织中不可或缺的一部分,特别是在需要处理大量并发连接和高流量的环境中。

特点

  1. 高性能

    • HAProxy 能够处理成千上万的并发连接,使其成为大规模网站的理想选择。
    • 它利用了高效的事件驱动模型,能够有效处理大量的网络请求。
  2. 高可用性

    • HAProxy 支持健康检查机制,能够监测后端服务器的健康状况。
    • 它能够快速地将故障的服务器负载均衡池中剔除,确保流量只被转发到健康的服务器上。
  3. 可配置性

    • HAProxy 提供了丰富的配置选项,允许管理员微调各种设置。
    • 它支持多种负载均衡算法,可以根据具体需求选择最合适的算法。
  4. 扩展性

    • HAProxy 可以轻松地与现有的基础设施集成,并且可以通过集群来进一步提升可用性和性能。
  5. 安全性

    • 它支持 SSL/TLS 加密,可以保护传输的数据安全。
    • HAProxy 可以用作防火墙的一部分,过滤恶意流量。

功能

  1. 负载均衡

    • HAProxy 可以根据预定义的规则和策略将传入的流量分发到多个后端服务器
    • 它支持多种负载均衡算法,如轮询、最少连接数、源地址哈希等。
  2. 健康检查

    • HAProxy 可以定期向后端服务器发送健康检查请求,以确定它们是否正常工作。
    • 根据健康检查的结果,它可以自动将流量重定向到健康的服务器
  3. 会话持久性

    • HAProxy 支持基于源 IP 地址或特定会话标识符的会话持久性。
    • 这有助于确保来自同一客户端的所有请求都被定向到同一后端服务器
  4. SSL/TLS 终止

    • HAProxy 可以作为 SSL/TLS 终结点,这意味着它可以直接处理加密连接,减轻后端服务器的负担。
    • 它支持 SNI (Server Name Indication) 和 ALPN (Application-Layer Protocol Negotiation)。
  5. 访问控制

    • HAProxy 可以配置 ACL (Access Control List),以限制对特定资源的访问。
    • 它支持基于 IP 地址、HTTP 头部和其他条件的访问控制。
  6. 日志记录和监控

    • HAProxy 提供详细的日志记录功能,可用于监控系统的性能和行为。
    • 它还支持外部监控工具,如 StatsD 和 Graphite。

配置

HAProxy 的配置文件通常位于 /etc/haproxy/haproxy.cfg,包含以下几个主要部分:

  1. 全局设置

    • 配置全局参数,如日志级别、最大连接数等。
  2. 监听器

    • 定义 HAProxy 如何接收和处理传入的连接。
    • 通常配置端口、绑定地址和基本的安全设置。
  3. 后端

  4. 前端

    • 配置前端接口,定义如何接收客户端请求并将它们分发到后端服务器

结论

HAProxy 是一个强大而灵活的工具,非常适合用于构建高可用性和高性能的网络应用和服务。无论是简单的负载均衡还是复杂的流量管理和安全策略,HAProxy 都是一个值得信赖的选择。

haproxy负载均衡测试

环境准备:

主机名IP角色VMware网络硬件配置
haproxy172.25.254.100haproxy负载均衡机器一块NAT的网卡
webserver1172.25.254.10真实服务器一块NAT的网卡
webserver2172.25.254.20真实服务器一块NAT的网卡

haproxy主机操作

[root@haproxy ~]# dnf install haproxy -y[root@haproxy ~]# rpm -qc haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
# 修改内容如下
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webclusterbind *:80mode httpuse_backend webcluster-hostbackend webcluster-hostbalance roundrobinserver  web1    172.25.254.10:80server  web2    172.25.254.20:80# 还有一种写法,直接写listen#frontend webcluster
#   bind *:80
#   mode http
#   use_backend webcluster-host
#
#backend webcluster-host
#   balance roundrobin
#   server  web1    172.25.254.10:80
#   server  web2    172.25.254.20:80listen webclusterbind *:80mode httpbalance roundrobinserver web1 172.25.254.10:80server web2 172.25.254.20:80[root@haproxy ~]# systemctl restart haproxy.service # 测试
[root@haproxy ~]# for i in {1..10}
> do
> curl 172.25.254.100
> done
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
日志重定向操作
[root@haproxy ~]# vim /etc/rsyslog.confmodule(load="imudp") # needs to be done just once
input(type="imudp" port="514")# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local2.*                                                /var/log/haproxy.log[root@haproxy ~]# systemctl restart haproxy.service [root@haproxy log]# ll /var/log/haproxy.log
-rw------- 1 root root 1359  87 16:26 /var/log/haproxy.log
[root@haproxy log]# cat /var/log/haproxy.log 
Aug  7 16:26:29 localhost haproxy[2048]: Server static/static is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Aug  7 16:26:29 localhost haproxy[2048]: backend static has no server available!
Aug  7 16:26:29 localhost haproxy[2048]: Server app/app1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 3 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Aug  7 16:26:30 localhost haproxy[2048]: Server app/app2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 2 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Aug  7 16:26:30 localhost haproxy[2048]: Server app/app3 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Aug  7 16:26:31 localhost haproxy[2048]: Server app/app4 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Aug  7 16:26:31 localhost haproxy[2048]: backend app has no server available!

webserver1主机操作

[root@webserver1 ~]# dnf install nginx -y
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html[root@webserver1 ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

webserver2主机操作

[root@webserver2 ~]# dnf install -y nginx
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

haporxy相关配置测试

backup参数测速

haproxy主机操作
[root@haproxy ~]# dnf install httpd -y
[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf 
# 修改默认端口号
46 #Listen 12.34.56.78:80
47 Listen 8080[root@haproxy ~]# systemctl restart httpd.service
[root@haproxy ~]# echo "sorry,下班了" > /var/www/html/index.html[root@haproxy ~]# curl 172.25.254.100:8080
sorry,下班了[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen webclusterbind *:80mode httpbalance roundrobinserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1# backup指定,其它的真实服务器访问失败后,会访问我这个服务器 172.25.254.100:8080server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service # 访问测试
[root@haproxy ~]# curl 172.25.254.100
sorry,下班了
[root@haproxy ~]# curl 172.25.254.100
sorry,下班了
webserver1主机操作
[root@webserver1 ~]# systemctl stop nginx.service 
webserver2主机操作
[root@webserver2 ~]# systemctl stop nginx.service

disabled参数测速

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen webclusterbind *:80mode httpbalance roundrobin# disabled参数,下线指定真实服务器server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabledserver web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1server web_sorry 172.25.254.100:8080 backup# 测试
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
webserver1主机操作
[root@webserver1 ~]# systemctl start nginx.service
webserver2主机操作
[root@webserver2 ~]# systemctl start nginx.service 

redirect prefix 参数测试

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg listen webclusterbind *:80mode httpbalance roundrobinredirect prefix http://www.baidu.com/#server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled#server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1#server web_sorry 172.25.254.100:8080 backup[root@haproxy ~]# systemctl restart haproxy.service 
浏览器访问测试
image-20240810212131693

haporxy的热处理,注意:重启后会失效

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
globallog         127.0.0.1 local2chroot      /var/lib/haproxypidfile     /var/run/haproxy.pidmaxconn     4000user        haproxygroup       haproxydaemon# turn on stats unix socket# 开启socker热处理,并且以root账户的身份去处理stats socket /var/lib/haproxy/stats mode 600 level admin# utilize system-wide crypto-policiesssl-default-bind-ciphers PROFILE=SYSTEMssl-default-server-ciphers PROFILE=SYSTE[root@haproxy ~]# systemctl restart haproxy.service 
# 下载socat工具
[root@haproxy ~]# dnf install socat-1.7.4.1-5.el9.x86_64  -y[root@haproxy ~]# echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats 
2 (initial 2)[root@haproxy ~]# echo "get weight webcluster/web2" | socat stdio /var/lib/haproxy/stats 
1 (initial 1)[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats [root@haproxy ~]# echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats 
1 (initial 2)[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 
1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
2 webcluster 1 web1 172.25.254.10 2 0 1 2 218 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 172.25.254.20 2 0 1 1 103 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
5 app 1 app1 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
5 app 2 app2 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
5 app 3 app3 127.0.0.1 0 0 1 1 217 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
5 app 4 app4 127.0.0.1 0 0 1 1 216 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats 
Name: HAProxy
Version: 2.4.17-9f97155
Release_date: 2022/05/13
Nbthread: 2
Nbproc: 1
Process_num: 1
Pid: 2634
Uptime: 0d 0h05m04s
Uptime_sec: 304
Memmax_MB: 0
PoolAlloc_MB: 0
PoolUsed_MB: 0
PoolFailed: 0
Ulimit-n: 8037
Maxsock: 8037
Maxconn: 4000
Hard_maxconn: 4000
CurrConns: 0
CumConns: 40675
CumReq: 13
MaxSslConns: 0
CurrSslConns: 0
CumSslConns: 0
Maxpipes: 0
PipesUsed: 0
PipesFree: 0
ConnRate: 0
ConnRateLimit: 0
MaxConnRate: 0
SessRate: 0
SessRateLimit: 0
MaxSessRate: 0
SslRate: 0

haproxy多进程如何热处理

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg # turn on stats unix socket# 为每一个进程独立建立一个控制文件stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1   stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2# utilize system-wide crypto-policiesssl-default-bind-ciphers PROFILE=SYSTEMssl-default-server-ciphers PROFILE=SYSTEM# 开启多进程nbproc 2cpu-map 1 0cpu-map 2 1[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw------- 1 root root 0  89 11:40 stats
srw------- 1 root root 0  89 11:58 stats1
srw------- 1 root root 0  89 11:58 stats2

haproxy调度算法详解

HAProxy 提供了一系列灵活的调度算法来分配客户端请求到后端服务器。选择正确的调度算法对于保证良好的性能和高可用性至关重要。本文将逐一介绍这些算法,并探讨它们的特点和应用场景。

Round Robin (轮询)

描述

轮询是最简单的调度算法之一。它按顺序将请求发送到后端服务器列表中的每个服务器。当到达列表末尾时,从头开始循环。

配置示例
backend serversmode httpbalance roundrobinserver srv1 192.0.2.1:80 checkserver srv2 192.0.2.2:80 checkserver srv3 192.0.2.3:80 check
适用场景
  • 当所有后端服务器具有相似性能和处理能力时。
  • 当后端服务器资源分配均匀时。
注意事项
  • 如果后端服务器性能不一致,可能会导致负载不均。

Least Connections (最少连接数)

描述

最少连接数算法试图将新的请求分发给当前连接数最少的服务器。这样可以尽量平衡各个服务器上的活动连接数。

配置示例
backend serversmode httpbalance leastconnserver srv1 192.0.2.1:80 checkserver srv2 192.0.2.2:80 checkserver srv3 192.0.2.3:80 check
适用场景
  • 当后端服务器的性能或配置不同,导致处理能力有差异时。
  • 当需要动态调整服务器池时。
注意事项
  • 需要实时监控连接数,可能对性能有一定影响。

Source (源地址哈希)

描述

源地址哈希算法基于客户端 IP 地址进行哈希计算,然后将请求分配给哈希值对应的后端服务器。这种方法可以确保来自同一客户端的请求总是被定向到同一台服务器,实现会话粘滞性。

配置示例
backend serversmode httpbalance sourceserver srv1 192.0.2.1:80 checkserver srv2 192.0.2.2:80 checkserver srv3 192.0.2.3:80 check
适用场景
  • 需要保持客户端会话状态的应用程序。
  • 实现会话粘滞性以提高用户体验。
注意事项
  • 如果后端服务器的性能或配置不同,可能导致负载不均。
  • 适用于静态 IP 场景,对于动态 IP 或 NAT 环境可能不太适合。

URI (URI 哈希)

描述

URI 哈希算法基于 HTTP 请求的 URI 进行哈希计算,然后将请求分配给哈希值对应的后端服务器。这可以确保相同 URI 的请求总是被定向到同一台服务器

配置示例
backend serversmode httpbalance uriserver srv1 192.0.2.1:80 checkserver srv2 192.0.2.2:80 checkserver srv3 192.0.2.3:80 check
适用场景
  • 需要确保相同 URL 的请求始终被路由到同一服务器的应用程序。
  • 用于缓存优化或一致性哈希环。
注意事项
  • 可能会导致不均匀的负载分布,特别是在 URL 分布不均匀的情况下。
总结

选择合适的调度算法取决于你的应用程序需求和后端服务器的特性。在实际部署中,可以根据实际情况组合使用不同的调度算法,以达到最佳效果。

haproxy状态页

root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen statsmode httpbind *:9999stats enablestats refresh 3stats uri /statusstats auth shanxin:shanxin[root@haproxy ~]# systemctl restart haproxy.service

浏览器测试

image-20240810212410395 image-20240810212421857

haproxy的cookie会话保持

haproxy主机操作

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen webclusterbind *:80mode httpbalance roundrobin#redirect prefix http://www.baidu.com/#server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2#server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1#server web_sorry 172.25.254.100:8080 backup# 添加cookie功能cookie WEBCOOKIE insert nocache indirectserver web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 1server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1[root@haproxy ~]# systemctl restart haproxy.service # 访问测速
[root@haproxy ~]# curl -b WEBCOOKIE=lee1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=lee1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20

cookie添加参数解释

name:cookie的key名称,用于实现持久连接

insert:插入新的cookie,默认不插入cookie

indirect:如果客户端已经有了cookie,则不会再发生cookie信息

nocache:当client和haproxy之间又缓存服务器(如CDN)时,不允许中间缓存器缓存cookie,因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

haproxy的IP透传

环境准备:

主机名IP角色VMware网络硬件配置
haproxy172.25.254.100haproxy负载均衡机器一块NAT的网卡
webserver1172.25.254.10真实服务器一块NAT的网卡
webserver2172.25.254.20真实服务器一块NAT的网卡
client172.25.254.200测试主机一块NAT的网卡

七层透传(mode=http)nginx

IP透传的用途:web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg defaultsmode                    httplog                     globaloption                  httplogoption                  dontlognulloption http-server-close# IP透传的参数,需要保持开启option forwardfor       except 127.0.0.0/8[root@haproxy ~]# systemctl restart haproxy.service
webserver2主机(nginx)操作
[root@webserver2 ~]# > /var/log/nginx/access.log
[root@webserver2 ~]# cat /var/log/nginx/access.log
# 这里显示了真实访问主机的IP,透传成功
172.25.254.100 - - [10/Aug/2024:10:34:36 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.76.1" "172.25.254.200"

七层透传(mode=http)apache

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg defaultsmode                    httplog                     globaloption                  httplogoption                  dontlognulloption http-server-close# IP透传的参数,需要保持开启option forwardfor       except 127.0.0.0/8[root@haproxy ~]# systemctl restart haproxy.service
webserver1主机(apache)操作
[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf# 添加这个内容到这里,%{X-Forwarded-For}iLogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined[root@webserver1 ~]# systemctl restart httpd.service [root@webserver1 ~]# > /etc/httpd/logs/access_log 
[root@webserver1 ~]# cat /etc/httpd/logs/access_log 
# 这里显示了真实访问主机的IP,透传成功
172.25.254.200 172.25.254.100 - - [10/Aug/2024:10:42:53 +0800] "GET / HTTP/1.1" 403 27 "-" "curl/7.76.1"

四层透传(mode=tcp)nginx

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg listen webclusterbind *:80# 这里需要更改为tcpmode tcpbalance roundrobin#redirect prefix http://www.baidu.com/#server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2#server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1#server web_sorry 172.25.254.100:8080 backup#cookie WEBCOOKIE insert nocache indirectserver web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1# 这里需要加上send-proxyserver web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1[root@haproxy ~]# systemctl restart haproxy.service
webserver2主机(nginx)操作
[root@webserver2 ~]# vim /etc/nginx/nginx.conf
http {log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" '# 在访问日志中通过变量$proxy_protocol_addr 记录透传过来的客户端IP' "$proxy_protocol_addr"''"$http_user_agent" "$http_x_forwarded_for"';server {listen       80 proxy_protocol; # 需要添加这个,才可以通过四层代理访问此网站listen       [::]:80;server_name  _;root         /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;[root@webserver2 ~]# systemctl restart nginx.service [root@webserver2 ~]# cat /var/log/nginx/access.log
# 这里显示了真实访问主机的IP,透传成功
172.25.254.100 - - [10/Aug/2024:10:56:26 +0800] "GET / HTTP/1.1" 200 27 "-"  "172.25.254.200""curl/7.76.1" "-"
172.25.254.100 - - [10/Aug/2024:10:56:27 +0800] "GET / HTTP/1.1" 200 27 "-"  "172.25.254.200""curl/7.76.1" "-"

四层透传(mode=tcp)apache

基本不使用了

总结

  • 七层透传
    • nginx直接就可以实现透传,不需要额外的配置
    • apache需要添加在主配置文件中添加对应的格式才可以(一步操作)
  • 四次透传
    • nginx需要配置三个部分,主配置文件添加二块,haproxy配置文件添加一块(send-proxy)
    • apache的这部分内容内取消掉了,无法实现了
client主机操作
# 都是访问操作
[root@client ~]# curl 172.25.254.10
[root@client ~]# curl 172.25.254.20

haproxy的acl基本访问控制

环境准备:

主机名IP角色VMware网络硬件配置
haproxy172.25.254.100haproxy负载均衡机器一块NAT的网卡
webserver1172.25.254.10真实服务器一块NAT的网卡
webserver2172.25.254.20真实服务器一块NAT的网卡
client172.25.254.200测试主机一块NAT的网卡

haproxy主机操作

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg# 进行一下配置文件的编写
frontend webclusterbind *:80mode httpacl test  hdr_end(host) -i .orguse_backend webcluster-host if testdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service 

client主机测试

[root@client ~]# vim /etc/hosts 
[root@client ~]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.200  client.shanxin.org
172.25.254.10   www.shanxin.org
172.25.254.20   www.shanxin.com# 测试成功
[root@client ~]# curl www.shanxin.org
webserver1 - 172.25.254.10
[root@client ~]# curl www.shanxin.com
webserver2 - 172.25.254.20

haproxy的acl一些常用限制

环境准备:

主机名IP角色VMware网络硬件配置
haproxy172.25.254.100haproxy负载均衡机器一块NAT的网卡
webserver1172.25.254.10真实服务器一块NAT的网卡
webserver2172.25.254.20真实服务器一块NAT的网卡
client172.25.254.200测试主机一块NAT的网卡

基于域名的访问控制

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfgfrontend webclusterbind *:80mode http# acl访问控制acl domain hdr_dom(host) -i www.shanxin.orguse_backend webcluster-host if domaindefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service
client主机操作
[root@client ~]# curl www.shanxin.org
webserver1 - 172.25.254.10
[root@client ~]# curl www.shanxin.com
webserver2 - 172.25.254.20

基于IP的访问控制

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg frontend webclusterbind *:80mode httpacl ctrl_ip src 172.25.254.200 172.25.254.20 192.168.0.0/24use_backend webcluster-host if ctrl_ipdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service
client主机操作
[root@client ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
haproxy主机测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20

基于IP的访问控制之黑名单

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webclusterbind *:80mode httpacl ctrl_ip src 172.25.254.200 172.25.254.20 192.168.0.0/24# use_backend webcluster-host if ctrl_iphttp-request deny if ctrl_ipdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service 
client主机操作
[root@client ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
haproxy主机测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
在这个实验中172.25.254.200 – client客户端,172.25.254.20 – webserver2主机端

基于浏览器的访问控制

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg frontend webclusterbind *:80mode httpacl badwebrowers hdr_sub(User-Agent) -i curl wgethttp-request deny if badwebrowersdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service 
client主机操作
[root@client ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
浏览器访问测试
image-20240810214317464

基于文件后缀名来实现动静分离

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
frontend webclusterbind *:80mode httpacl static path_end -i .html .jpg .png .css .jsacl php path_end -i .phpuse_backend webcluster-host if phpdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service 
webserver1主机操作
[root@webserver1 ~]# dnf install php -y
[root@webserver1 ~]# systemctl disable --now nginx.service [root@webserver1 ~]# systemctl restart httpd.service 
[root@webserver1 ~]# vim /var/www/html/index.php
[root@webserver1 ~]# cat /var/www/html/index.php 
<?phpphpinfo();
?>
client主机操作
[root@client ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
浏览器访问控制
image-20240810214410482

基于网址后面的目录文件的访问控制

haproxy主机操作
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg frontend webclusterbind *:80mode httpacl static path_sub -m sub staticacl php    path_sub -m sub phpuse_backend webcluster-host if phpdefault_backend default-hostbackend webcluster-hostmode httpserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5backend default-hostmode httpserver web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service 
webserver1主机操作
[root@webserver1 ~]# mkdir /var/www/html/php/ -p
[root@webserver1 ~]# cp /var/www/html/index.php /var/www/html/php/
webserver2主机操作
[root@webserver2 ~]# mkdir /usr/share/nginx/html/static/ -p
[root@webserver2 static]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/static/
浏览器访问测试
image-20240810214518567

haproxy自定义错误页面

haproxy主机操作

# 查看错误页面文件的存储路径
[root@haproxy ~]# rpm -ql haproxy | grep http
/usr/share/doc/haproxy/design-thoughts/http2.txt
/usr/share/doc/haproxy/design-thoughts/http_load_time.url
/usr/share/doc/haproxy/internals/http-cookies.txt
/usr/share/doc/haproxy/internals/http-docs.txt
/usr/share/doc/haproxy/internals/http-parsing.txt
/usr/share/doc/haproxy/option-http_proxy.cfg
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http[root@haproxy ~]# mkdir  /etc/haproxy/errorpage/
[root@haproxy ~]# cp /usr/share/haproxy/503.http /etc/haproxy/errorpage/503.http
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http
# 修改内容如下
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html<html><body><h1>errrrrrrrrrrrr</h1>
No server is available to handle this request.
</body></html># 配置haproxy主配置文件
[root@haproxy conf.d]# vim /etc/haproxy/haproxy.cfg timeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 3000# 这条语句要放在default里面errorfile   503 /etc/haproxy/errorpage/503.http[root@haproxy conf.d]# systemctl restart haproxy.service 

webserver1主机操作

[root@webserver1 ~]# systemctl stop httpd.service 

webserver2主机操作

[root@webserver2 static]# systemctl stop nginx.service

client主机操作

[root@client ~]# curl 172.25.254.100
<html><body><h1>errrrrrrrrrrrr</h1>
No server is available to handle this request.
</body></html>[root@client ~]# curl 172.25.254.10
curl: (7) Failed to connect to 172.25.254.10 port 80: 拒绝连接
[root@client ~]# curl 172.25.254.20
curl: (7) Failed to connect to 172.25.254.20 port 80: 拒绝连接

浏览器访问测试

image-20240810214638935

haproxy报错后跳转指定网站

haproxy主机操作

[root@haproxy conf.d]# vim /etc/haproxy/haproxy.cfg #errorfile  503 /etc/haproxy/errorpage/503.http # 也是在defualt语句块中errorloc    503 https://www.baidu.com
[root@haproxy conf.d]# systemctl restart haproxy.service 

浏览器测试

image-20240810214710214

haproxy四层负载数据库

haproxy主机操作

# 在haproxy的子配置文件中配置
[root@haproxy conf.d]# vim /etc/haproxy/conf.d/web.cfg
[root@haproxy conf.d]# cat /etc/haproxy/conf.d/web.cfg
listen dbserverbind *:3306mode tcpbalance static-rrserver db1      172.25.254.10:3306 check inter 2 fall 2 rise 5server db2      172.25.254.20:3306 check inter 2 fall 2 rise 5[root@haproxy conf.d]# systemctl restart haproxy.service 

webserver1主机操作

[root@webserver1 ~]# dnf install mariadb-server -y
[root@webserver1 ~]# systemctl enable --now mariadb
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf [mysqld]
server-id=1 # 设置数据库的id为了在之后的测试方便
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid[root@webserver1 ~]# systemctl restart mariadb.service [root@webserver1 ~]# mysql # 建立普通用户lee,并授予权限,为了之后的远程登录测试
MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant all on *.* to lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> quit
Bye

webserver2主机操作

[root@webserver2 ~]# dnf install mariadb-server -y
[root@webserver2 ~]# systemctl enable --now mariadb
[root@webserver2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf[mysqld]
server-id=2 # server-id=1 # 设置数据库的id为了在之后的测试方便
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid[root@webserver2 ~]# systemctl restart mariadb.service [root@webserver2 ~]# mysql 
MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant all on *.* to lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> quit
Bye

client主机操作

[root@client ~]# mysql -ulee -plee -h 172.25.254.100
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.001 sec)
MariaDB [(none)]> quit
Bye[root@client ~]# mysql -ulee -plee -h 172.25.254.100
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.001 sec)
MariaDB [(none)]> quit
Bye[root@client ~]# mysql -ulee -plee -h 172.25.254.100
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.001 sec)
MariaDB [(none)]> quit
Bye[root@client ~]# mysql -ulee -plee -h 172.25.254.100
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.001 sec)

haproxy之https网站加密

haproxy主机操作

# 建立证书以及密钥的存储目录
[root@haproxy ~]# mkdir -p /etc/haproxy/certs
# 生成证书以及密钥
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/shanxin.org.key -x509 -days 365 -out /etc/haproxy/certs/shanxin.org.crt[root@haproxy ~]# ll /etc/haproxy/certs/
总用量 8
-rw-r--r-- 1 root root 1415  810 17:19 shanxin.org.crt
-rw------- 1 root root 1708  810 17:18 shanxin.org.key# 将证书以及密钥文件都存储在配置默认路径中
[root@haproxy ~]# cat /etc/haproxy/certs/shanxin.org.key /etc/haproxy/certs/shanxin.org.crt > /etc/haproxy/certs/shanxin.org.pem
[root@haproxy ~]# ll /etc/haproxy/certs/shanxin.org.pem 
-rw-r--r-- 1 root root 3123  810 17:21 /etc/haproxy/certs/shanxin.org.pem[root@haproxy ~]# vim /etc/haproxy/conf.d/web.cfg listen web-httpsbind *:443 ssl crt /etc/haproxy/certs/shanxin.org.pemmode httpbalance roundrobinserver web1 172.25.254.10:80 check inter 2 fall 2 rise 5 # 这里实际上是对haproxy主机进行了https加密,真实的服务器还是80端口,未加密server web2 172.25.254.20:80 check inter 2 fall 2 rise 5[root@haproxy ~]# systemctl restart haproxy.service 

webserver1主机操作

[root@webserver1 ~]# systemctl start httpd.service

webserver2主机操作

[root@webserver2 ~]# systemctl start nginx.service

浏览器访问测试

image-20240810214911318 image-20240810214921771

image-20240810214936005

haproxy之https网站强制全站加密

haproxy主机操作

[root@haproxy ~]# vim /etc/haproxy/conf.d/web.cfg listen webclusterbind *:80mode httpredirect scheme https if !{ ssl_fc }
n
[root@haproxy ~]# systemctl restart haproxy.service 

浏览器访问测试

image-20240810215005944

这里在地址栏中原本输入的是172.25.254.100,但最终访问的是https://172.25.254.100


http://www.ppmy.cn/news/1508678.html

相关文章

Solidity面试题,由浅入深

Solidity是Ethereum智能合约的主要编程语言&#xff0c;面试题的设计旨在评估候选人对Solidity语言特性的掌握程度&#xff0c;以及他们对区块链和智能合约的理解。下面列出了一些常见的Solidity面试题&#xff0c;涵盖基础知识到高级概念&#xff0c;并简要说明每个问题的答案…

修改docker的/var/lib/docker/overlay2储存路径

目录 目录 1.准备新的存储位置 1.创建新的存储目录 2.修改目录权限 2. 配置 Docker 使用新的存储位置 1.停止 Docker 服务 2.编辑 Docker 配置文件 3.迁移现有 Docker 数据 1.将现有的 Docker 数据从系统盘移动到新目录 2.启动 Docker 服务 3. 验证更改 4. 清理旧的…

节流函数:JavaScript性能优化的利器

标题&#xff1a;节流函数&#xff1a;JavaScript性能优化的利器 在JavaScript开发中&#xff0c;我们经常会遇到一些频繁触发的操作&#xff0c;如窗口调整大小、滚动条滚动等。这些操作如果处理不当&#xff0c;可能会导致性能问题&#xff0c;甚至引起浏览器的卡顿。节流&a…

【机器学习之深度学习】神经元、层的基本概念,神经网络以及神经元与线性回归和逻辑回归的相似之处

引言 神经元、层和神经网络是深度学习模型的核心组成部分&#xff0c;它们共同工作&#xff0c;使得深度学习能够处理复杂的数据和任务。通过叠加多个层&#xff0c;可以构建出能够学习复杂函数的深度神经网络 一、神经元和层 深度学习中的神经元和层是构建复杂预测模型的基础…

多模态大语言模型(MMLLM)的现状、发展和潜力

1、大模型 随着ChatGPT流行&#xff0c;大模型技术正逐渐成为AI领域的热点。许多行业大佬纷纷投身于这一赛道&#xff0c;展示了大模型的独特魅力和广阔前景。 王慧文&#xff0c;前美团联合创始人&#xff0c;发起“AI英雄帖”。 李志飞&#xff0c;出门问问创始人&#xff0…

Linux LD_PRELOAD优先加载so失效原因分析

网上由很多介绍LD_PRELOAD劫持的文章&#xff0c;我就不做过多介绍&#xff0c;之前有碰到失效的&#xff0c;网上找了很久没找到原因&#xff0c;后面分析出原因&#xff0c;现在写出来给后人避坑。 Linux系统使用LD_PRELOAD环境变量可以让程序优先加载指定的so文件&#xff0…

Nginx系列-Nginx Location匹配规则

文章目录 Nginx系列-Nginx Location匹配规则1. 语法基础2. 匹配规则2.1 精确匹配&#xff08;&#xff09;2.2. 最长前缀匹配&#xff08;^~&#xff09;2.3. 正则表达式匹配&#xff08;~和~*&#xff09;2.4. 普通前缀匹配&#xff08;无修饰符&#xff09;2.5. 默认匹配&…

【数据结构-前缀哈希】力扣3026. 最大好子数组和

给你一个长度为 n 的数组 nums 和一个 正 整数 k 。 如果 nums 的一个 子数组 中&#xff0c;第一个元素和最后一个元素 差的绝对值恰好 为 k &#xff0c;我们称这个子数组为 好 的。换句话说&#xff0c;如果子数组 nums[i…j] 满足 |nums[i] - nums[j]| k &#xff0c;那么…

易基因:儿童和成人实体瘤共有微小差异甲基化区域(mDMR)的全面分析 | 表观研究

大家好&#xff0c;这里是专注表观组学十余年&#xff0c;领跑多组学科研服务的易基因。 癌症是美国1~14岁儿童第二大常见死因&#xff0c;每年约有11000例新发病例和1200例死亡病例。与成人癌症相比&#xff0c;儿童肿瘤通常突变负荷较低。然而儿童肿瘤的表观基因组发生显著变…

LVS的简单配置及对Mysql主从复制的补充

Day 22 LVS的配置 环境准备 DSN() 用来解析各主机的域名和ip地址&#xff0c;配置域名解析huajuan&#xff0c;负责管理其他主机 web1--->web1.tangpin.huajuan web2--->web2.tangpin.huajuan dns--->dns.tangpin.huajuan web1(192.168.2.200) 用nginx…

‘Task‘ object is not callable ERROR

pycharm 调试异步的代码报错 TypeError: ‘Task‘ object is not callable ERROR: Exception in callback &#xff1c;Task 解决方法&#xff1a;点击菜单栏帮助-查找操作-注册表&#xff0c;在注册表中搜索python.debug.asyncio.repl禁用即可

<Qt> 系统 - 事件

目录 前言&#xff1a; 一、事件介绍 二、事件的处理 &#xff08;一&#xff09;鼠标事件 1. 进入和离开事件 2. 鼠标点击事件 3. 释放事件 4. 双击事件 5. 移动事件 6. 滚轮事件 &#xff08;二&#xff09;键盘按键事件 1. 单个按键 2. 组合按键 &#xff08;…

交叉编译util-linux

参考文章&#xff1a;https://www.cnblogs.com/wanglouxiaozi/p/17836701.html 1、下载源码 https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.39/2、编译 解压压缩包&#xff1a; sudo tar xvf util-linux-2.39.2.tar.gz执行autogen.sh生成configure ./aut…

书生.浦江大模型实战训练营——(四)书生·浦语大模型全链路开源开放体系

最近在学习书生.浦江大模型实战训练营&#xff0c;所有课程都免费&#xff0c;以关卡的形式学习&#xff0c;也比较有意思&#xff0c;提供免费的算力实战&#xff0c;真的很不错&#xff08;无广&#xff09;&#xff01;欢迎大家一起学习&#xff0c;打开LLM探索大门&#xf…

NPM版本管理高级技巧:实现版本范围预览锁定

引言 在现代软件开发中&#xff0c;依赖管理是确保项目稳定性和可预测性的关键。NPM&#xff08;Node Package Manager&#xff09;作为Node.js生态系统的包管理器&#xff0c;提供了一套灵活的版本控制机制&#xff0c;允许开发者精确控制依赖包的版本。版本范围预览锁定是一…

求1000以内的水仙花数【C语言】

求1000以内的水仙花数 #include <stdio.h> //包含标准输入输出头文件&#xff0c;用于使用printf函数int main() { //程序的主函数开始int a, b, c, i; //i用于循环遍历100到999之间的所有数&#xff08;三位数&#xff09;&#xff0c;a, b, c分别用于存储当前数i的百位…

汽车电子中间件的关键技术

汽车电子中间件的关键技术 中间件架构设计分层架构与模块化设计优势劣势 服务导向架构&#xff08;SOA&#xff09;主要特点&#xff1a;SOA在汽车电子中的应用&#xff1a;优势&#xff1a;劣势&#xff1a; 通讯协议与数据传输传统协议&#xff08;CAN、LIN&#xff09;CAN&a…

Github 2024-08-09 开源项目日报 Top10

根据Github Trendings的统计,今日(2024-08-09统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量Python项目6TypeScript项目4Jupyter Notebook项目1Cuda项目1Sentry:开发者优先的错误跟踪和性能监控平台 创建周期:5093 天开发语言:Python,…

python 实现Aho-Corasick(AC)算法

Aho-Corasick&#xff08;AC算法&#xff09;算法介绍 Aho-Corasick&#xff08;AC&#xff09;算法是一种高效的字符串多模匹配算法&#xff0c;由Alfred V. Aho和Margaret J. Corasick在1975年提出。该算法通过构建一个有限状态自动机&#xff08;AC自动机&#xff09;来实现…

Python | Leetcode Python题解之第326题3的幂

题目&#xff1a; 题解&#xff1a; class Solution:def isPowerOfThree(self, n: int) -> bool:return n > 0 and 1162261467 % n 0