本文主要介绍 Apache 的实际使用,文中所使用到的软件版本:Centos 7.9.2009、Httpd 2.4.55。
1、反向代理
涉及到 Https 站点时,安装 Apache 时需要启用 ssl,可参考 Apache httpd 入门实战(1)--概念及安装。
1.1、被代理站点为 Http 站点
打开 conf/httpd.conf 文件,修改或新增相应的配置项:
Listen 8080 ServerName 10.49.196.33:8080 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so include conf/extra/my-vhosts.conf
新建 conf/extra/my-vhosts.conf 文件:
<VirtualHost *:8080> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common ProxyPass /qwe http://10.49.196.1:9090/qwe ProxyPassReverse /qwe http://10.49.196.1:9090/qwe </VirtualHost>
代理地址为 http://10.49.196.33:8080/qwe,被代理的地址为 http://10.49.196.1:9090/qwe。
1.2、被代理站点为 Https 站点
打开 conf/httpd.conf 文件,修改或新增相应的配置项:
Listen 8080 ServerName 10.49.196.33:8080 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule ssl_module modules/mod_ssl.so include conf/extra/my-vhosts.conf
新建 conf/extra/my-vhosts.conf 文件:
<VirtualHost *:8080> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common SSLProxyEngine On ProxyPass /asd https://10.49.196.1:9091/asd ProxyPassReverse /asd https://10.49.196.1:9091/asd </VirtualHost>
代理地址为 http://10.49.196.33:8080/asd,被代理的地址为 https://10.49.196.1:9091/asd。
1.3、代理站点启用 Https
1.3.1、生成证书
代理站点启用 Https,需要使用 OpenSSL 创建证书。
A、生成根证书
openssl genrsa -out ca.key openssl req -new -key ca.key -out ca.csr openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.pem
B、生成服务端证书
openssl genrsa -out server.key openssl req -new -key server.key -out server.csr openssl ca -days 3650 -in server.csr -cert ca.pem -keyfile ca.key -out server.pem
C、生成客户端证书
openssl genrsa -des3 -out client.key 1024 openssl req -new -key client.key -out client.csr openssl ca -days 1500 -in client.csr -cert ca.pem -keyfile ca.key -out client.pem openssl pkcs12 -export -clcerts -in client.pem -inkey client.key -out client.p12
使用 OpenSSL 创建证书的详细说明可参考:OpenSSL 介绍(5)--数字证书;这里生成的证书假设都存放在 /home/mongo/ssl 目录下。
1.3.2、Apache 中配置 Https
打开 conf/httpd.conf 文件,修改或新增相应的配置项:
Listen 8443 ServerName 10.49.196.33:8443 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule ssl_module modules/mod_ssl.so include conf/extra/my-vhosts.conf
新建 conf/extra/my-vhosts.conf 文件:
<VirtualHost *:8443> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common SSLEngine on SSLCertificateFile /home/mongo/ssl/server.pem SSLCertificateKeyFile /home/mongo/ssl/server.key ProxyPass /qwe http://10.49.196.1:9090/qwe ProxyPassReverse /qwe http://10.49.196.1:9090/qwe SSLProxyEngine On ProxyPass /asd https://10.49.196.1:9091/asd ProxyPassReverse /asd https://10.49.196.1:9091/asd </VirtualHost>
代理地址为 https://10.49.196.33:8443/qwe、https://10.49.196.33:8443/asd,被代理的分别地址为 http://10.49.196.1:9090/qwe、https://10.49.196.1:9091/asd。
如果需要验证客户端,则配置如下:
<VirtualHost *:8443> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common SSLEngine on SSLCertificateFile /home/mongo/ssl/server.pem SSLCertificateKeyFile /home/mongo/ssl/server.key SSLVerifyClient require SSLCACertificateFile /home/mongo/ssl/ca.pem ProxyPass /qwe http://10.49.196.1:9090/qwe ProxyPassReverse /qwe http://10.49.196.1:9090/qwe SSLProxyEngine On ProxyPass /asd https://10.49.196.1:9091/asd ProxyPassReverse /asd https://10.49.196.1:9091/asd </VirtualHost>
2、负载均衡
打开 conf/httpd.conf 文件,修改或新增相应的配置项:
Listen 8080 ServerName 10.49.196.33:8080 LoadModule watchdog_module modules/mod_watchdog.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_hcheck_module modules/mod_proxy_hcheck.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so include conf/extra/my-vhosts.conf
2.1、不考虑 session
新建 conf/extra/my-vhosts.conf 文件:
<VirtualHost *:8080> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common <Proxy balancer://mycluster> BalancerMember http://10.49.196.1:9090 loadfactor=2 BalancerMember http://10.49.196.2:9090 loadfactor=3 timeout=3 ProxySet lbmethod=byrequests </Proxy> ProxyPass /xyz balancer://mycluster/xyz ProxyPassReverse /xyz balancer://mycluster/xyz </VirtualHost>
lbmethod:负载均衡算法
lbmethod=byrequests:按请求次数负载均衡
lbmethod=bytraffic:按流量负载均衡
lbmethod=bybusyness:按繁忙程度(挂起的请求数)负载均衡
lbmethod=heartbeat:按心跳数负载均衡
loadfactor:负载因子;timeout:超时时间,单位为秒。
代理地址为 http://10.49.196.33:8080/xyz,被代理的地址为 http://10.49.196.1:9090/xyz、http://10.49.196.2:9090/xyz。
2.2、session 粘滞
对应有 session 的情况,同一客户端需要把请求转发到同一个后端服务。新建 conf/extra/my-vhosts.conf 文件:
<VirtualHost *:8080> ServerAdmin admin@abc.com ServerName abc.com ServerAlias abc.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common <Proxy balancer://mycluster> Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/tsp" env=BALANCER_ROUTE_CHANGED BalancerMember http://10.49.196.1:9090 loadfactor=2 route=server1 BalancerMember http://10.49.196.2:9090 loadfactor=3 route=server2 ProxySet lbmethod=byrequests ProxySet stickysession=ROUTEID </Proxy> ProxyPass /xyz balancer://mycluster/xyz ProxyPassReverse /xyz balancer://mycluster/xyz </VirtualHost>
Header add Set-Cookie:设置名称为 ROUTEID 的 cookie
ProxySet stickysession=ROUTEID:根据 cookie ROUTEID 的值路由到相应的后端服务
2.3、failover
<Proxy balancer://myset> BalancerMember http://www2.example.com:8080 BalancerMember http://www3.example.com:8080 loadfactor=3 timeout=1 BalancerMember http://spare1.example.com:8080 status=+R BalancerMember http://spare2.example.com:8080 status=+R BalancerMember http://hstandby.example.com:8080 status=+H BalancerMember http://bkup1.example.com:8080 lbset=1 BalancerMember http://bkup2.example.com:8080 lbset=1 ProxySet lbmethod=byrequests </Proxy> ProxyPass "/images/" "balancer://myset/" ProxyPassReverse "/images/" "balancer://myset/"
A、http://www2.example.com:8080 或 http://www3.example.com:8080 不可用时,才会把流量分配给 http://spare1.example.com:8080 和 http://spare2.example.com:8080。一个备用的节点会替换一个不可用的节点。
B、当 http://www2.example.com:8080、http://www2.example.com:8080、http://www2.example.com:8080、http://www2.example.com:8080 都不可用时,才会把流量分配给热备节点:http://hstandby.example.com:8080。
C、当所有 lbset=0(后端节点的优先级,默认为 0;数值越低的,优先级越高) 的节点(正常节点、备用节点、热备节点)都不可用时,才会把流量分配给备份节点:http://bkup1.example.com:8080 和 http://bkup2.example.com:8080。
2.4、负载均衡管理器
<VirtualHost *:8080> ... <Location "/balancer-manager"> SetHandler balancer-manager Require ip 10.49.196.35 </Location> ... </VirtualHost>
Require ip:设置可用访问负载均衡器的 ip。
通过负载均衡管理页面(http://10.49.196.33:6060/balancer-manager)可以查看负载均衡的配置和状态,还可以在页面上直接修改配置。
文章来源地址https://www.toymoban.com/news/detail-415532.html
参考:https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html。文章来源:https://www.toymoban.com/news/detail-415532.html
到了这里,关于Apache httpd 入门实战(2)--简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!