keepalive+Nginx反向代理实现apache服务的负载均衡

Nginx源码安装

Nginx源码包:链接: Nginx-1.19.1 提取码: x4cg
源码安装步骤:

  1. tar zxf nginx-1.19.1.tar.gz

  2. 解决nginx源码编译的依赖性 yum install gcc pcre-devel openssl-devel -y

  3. 检测安装环境./configure --prefix=/usr/local/nginx --with-http_ssl_module --prefix= 参数后跟的是安装路径

  4. Makefile文件所在的目录,进行make && make install

  5. 为了使安装后的文件更小,可以不需要gcc中的debug参数,nginx解压目录 下的auto/cc/gcc在这里插入图片描述

  6. 可以通过修改nginx 解压目录下的 src/core/nginx.h 来使版本号只显示nginx在这里插入图片描述

  7. 再次执行 3和4 的步骤,进行源码安装,安装后的目录就很小
    在这里插入图片描述

  8. ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx 可以使nginx 直接在命令行中调用

调用方法作用
nginx打开nginx服务
nginx -t检测nginx的配置文件有没有语法错误
nginx -s stop关闭nginx服务
nginx -V显示详细信息
  1. 建立/usr/lib/systemd/system/nginx.service,文件内容如下,就可以通过所以systemctl 的方式启动nginx
    在这里插入图片描述

利用Nginx反向代理实现apache服务的负载均衡

轮叫调度算法(Round Robin 默认使用)

实现步骤:

  1. 建立nginx用户useradd -s /sbin/nologin nginx 作为nginx 中的usr, 其中worker_connections 和内核,系统, app 有关,内核用sysctl -a | grep file可以查看允许的最大连接数是多少,和系统的内存有关,系统用ulimit -a可以查看到系统允许的当前用户的open files,默认是1024,所以通过修改/etc/security/limits.conf来指定nginx 用户打开文件的个数。在这里插入图片描述在这里插入图片描述
  1. 编辑nginx配置文件vim/usr/local/nginx/conf/nginx.conf
    在这里插入图片描述在这里插入图片描述
  1. systemctl start nginx 在宿主机的/etc/hosts中配置 解析 172.25.3.2 www.westos.org ,事先在server3和server4 apache的测试页中放入了主机名,当server3和server4的http服务打开之后,会看到由轮叫算法调度,访问10次,依次交替出现server3和server4。
    在这里插入图片描述
    由于nginx 自带会对后端服务器进行健康检查,所以说如果后端的某一个服务器down掉之后,客户端访问的时候就不会访问那个被down掉的服务器
    在这里插入图片描述
  1. 在定义组的时候增加权重
    在这里插入图片描述
    测试:在这里插入图片描述

最小连接调度算法

一个请求以最少的活动连接数发送到服务器,同样考虑服务器权重:
实验配置:
只用在upstream 定义组的时候加入least_conn,其他的配置都一样
在这里插入图片描述

IP_HASH 算法

请求发送到的服务器由客户端IP地址决定。在这种情况下,使用IPv4地址的前三个八进制数或整个IPv6地址来计算散列值。该方法保证来自相同地址的请求能够到达相同的服务器,除非该服务器不可用。
实验配置: 在upstream 定义组的时候加入ip_hash,将backup 注释
在这里插入图片描述
测试:在这里插入图片描述

sticky 调度算法

通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route
需要下载模块:链接: sticky模块 提取码: piqd
模块导入步骤:

  1. 解压下载好的zip包(我的是和nginx解压目录一个目录下)unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
  2. 在nginx的安装目录(/root/nginx-1.19.1)make clean
  3. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42 检测安装环境是否完备,–add-module 后面跟的是nginx模块解压目录的路径
  4. make && make install 安装

重启服务测试:
在这里插入图片描述
会一直访问一个服务器,只到删除浏览器的缓存,才会访问另一个浏览器

Keepalive + Nginx

目的:实现Nginx的高可用

实验步骤:

  1. 将server2 上完整的nginx 目录复制到serever1上,并在server1上做和server2一样的配置。启动nginx查看是否能访问。
  1. 两边配置keepalive 配置文件,都需要检测脚本来实现对nginx服务的健康检测, vim /opt/check_nginx.sh , 记得给该脚本添加 +x 权限 chomod +x /opt/check_nginx.sh
    在这里插入图片描述
  1. 修改server1 和server2的keepalive配置文件
    server1:
    在这里插入图片描述
    server2:
    在这里插入图片描述
  1. 测试: 修改宿主机的‘www.westos.org’对应的解析地址为vip地址,即:172.25.3.100 www.westos.org
    当停止一个节点的nginx服务时,虚拟ip会自动漂移到另一个节点上,当客户端访问时并不会影响在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,tomcat8的配置: 1.1修改tomcat8.x/conf/context.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> --> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" sessionBackupTimeout="18000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context> 1.2添加memcached如下依赖的jar包到tomcat8.x/lib/: asm-5.1.jar couchbase-client-1.4.12.jar javolution-5.5.1.jar kryo-4.0.0.jar kryo-serializers-0.38.jar memcached-session-manager-2.0.0.jar memcached-session-manager-tc8-2.0.0.jar minlog-1.3.jar msm-javolution-serializer-2.0.0.jar msm-kryo-serializer-2.0.0.jar msm-xstream-serializer-2.0.0.jar objenesis-2.1.jar reflectasm-1.09.jar spymemcached-2.12.1.jar 2,nginx的配置: #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; 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 logs/access.log main; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; #设定负载均衡服务器列表 upstream 127.0.0.1 { #设定负载均衡服务器列表 #ip_hash; #同一机器在多网情况下,路由切换,ip可能不同 #weigth参数表示权值,权值越高被分配到的几率越大 server 127.0.0.1:8085 weight=1 max_fails=20 fail_timeout=600s; server 127.0.0.1:8086 weight=1 max_fails=20 fail_timeout=600s; } map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; server_name localhost; charset UTF-8; #设定本虚拟主机的访问日志 access_log logs/host.access.log main; #对 "/" 所有应用启用负载均衡 location / { proxy_pass http://127.0.0.1; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } #对 "/Dossm3RabbitMQConsumer/" 启用负载均衡 location /Dossm3RabbitMQConsumer/ { proxy_pass http://localhost:8086; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 3,cas的配置(): 3.1 修改/CAS/WEB-INF/spring-configuration/ticketRegistry.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to Jasig under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. Jasig licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at the following location: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <description> Configuration for the default TicketRegistry which stores the tickets in-memory and cleans them out as specified intervals. </description> <!-- memcached 配置开始 --> <!-- Ticket Registry --> <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.MemCacheTicketRegistry"> <constructor-arg index="0"> <bean class="net.spy.memcached.spring.MemcachedClientFactoryBean" p:servers="127.0.0.1:11211" p:protocol="BINARY" p:locatorType="ARRAY_MOD" p:failureMode="Redistribute" p:transcoder-ref="serialTranscoder"> <property name="hashAlg"> <util:constant static-field="net.spy.memcached.DefaultHashAlgorithm.FNV1A_64_HASH" /> </property> </bean> </constructor-arg> <!-- TGT timeout in seconds --> <constructor-arg index="1" value="36000" /> <!-- ST timeout in seconds --> <constructor-arg index="2" value="2" /> </bean> <bean id="serialTranscoder" class="net.spy.memcached.transcoders.SerializingTranscoder" p:compressionThreshold="2048" /> <!-- memcached 配置结束 --> <!--Quartz --> <!-- 默认配置开始 --> <!-- Ticket Registry --> <!-- <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />--> <!-- TICKET REGISTRY CLEANER --> <!-- <bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner" p:ticketRegistry-ref="ticketRegistry" p:logoutManager-ref="logoutManager" /> <bean id="jobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" p:targetObject-ref="ticketRegistryCleaner" p:targetMethod="clean" /> <bean id="triggerJobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.SimpleTriggerBean" p:jobDetail-ref="jobDetailTicketRegistryCleaner" p:startDelay="200000" p:repeatInterval="50000000" /> --> <!-- 默认配置结束 --> </beans> 3.2 添加cas和memcached整合的如下依赖jar包到/CAS/WEB-INF/lib: cas-server-integration-memcached-4.0.0.jar mockito-core-2.1.0-RC.1.jar spymemcached-2.11.2.jar 参考CAS官方配置:https://apereo.github.io/cas/4.2.x/installation/Memcached-Ticket-Registry.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值