[每周一更]-(第79期):Apache代理的配置

本文介绍Apache作为反向代理服务器的配置方法,包括SSL/TLS加密、负载均衡及关键指令说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

反向代理逻辑类似Nginx,以下具体展示属于apache的配置和参数说明

局部代理配置方式:

# 配置包含https的需要打开
SSLProxyEngine on
ProxyPass /api/small https://api.web.com/version1/small/
ProxyPassReverse /api/small https://api.web.com/version1/small/

配置80端口

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/var/www/html/gkmobile/public"
    ServerName web.com
    ServerAlias *.web.com
    #RedirectMatch ^/$ https://*.web.com
    ErrorLog "logs/web.com-error_log"
    CustomLog "logs/web.com-access_log" common

    ProxyPass /api.php/ http://$host_ip.web.com/
    ProxyPassReverse /api.php/ http://$host_ip.web.com/
</VirtualHost>

配置443端口,详细的关于PHP Laravel项目,泛域名中配置证书+反向代理+请求头


# web 泛域名
<VirtualHost *:443>
    DocumentRoot "/var/www/html/gkmobile/public"
    ServerName web.com:443
    ServerAlias *.web.com:443
    Header set Access-Control-Allow-Origin "http://x.x.x.x"
    Header set Access-Control-Allow-Origin "http://xx.web.cn"
    Header set Access-Control-Allow-Origin "https://xx.web.net"

    ErrorLog "logs/web.com-error_log"
    CustomLog "logs/web.com-access_log" common
    LogLevel warn

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    SSLCertificateFile /etc/httpd/conf/web-ssl/web.com.cer
    SSLCertificateKeyFile /etc/httpd/conf/web-ssl/web.com.key
    SSLCertificateChainFile /etc/httpd/conf/web-ssl/fullchain.cer

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
    </Files>

    <Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-5]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0

    CustomLog logs/ssl_request_log \
    "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

    SSLProxyEngine on
    ProxyPass /api/small https://api.web.com/version1/small/
    ProxyPassReverse /api/small https://api.web.com/version1/small/

</VirtualHost>

说明

apache 版本

httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 16 2020 16:18:20

参数

Apache 的反向代理配置主要使用 mod_proxy 模块,其中包括一些重要的指令用于定义代理规则。以下是一些常用的反向代理指令及其说明:

  • ProxyPass:

说明: 用于定义需要被代理的 URL。
示例:
ProxyPass "/app" "http://backend-server/"

  • ProxyPassReverse:

说明: 用于修正后端服务器发送的 HTTP 头中的 Location 和 URI 等信息,确保客户端能正确解析。
示例:

ProxyPassReverse "/app" "http://backend-server/"
  • ProxyPreserveHost:

说明: 用于保留客户端发送的 Host 头,防止后端服务器收到的请求中 Host 头被修改。
示例:

ProxyPreserveHost On
  • ProxyRequests

说明: 当你希望将 Apache 服务器配置为一个正向代理服务器时,可以使用 ProxyRequests 指令。正向代理通常用于为客户端提供对互联网的访问,或者为内部网络的用户提供外部资源访问的能力。
示例:

ProxyRequests on | off(默认)

  • ProxyTimeout:

说明: 定义代理请求的超时时间。
示例:

ProxyTimeout 60
  • ProxyPassMatch:

说明: 允许使用正则表达式匹配 URL,并将匹配的请求代理到后端服务器。
示例:

ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://localhost:9000/var/www/html/"
  • ProxyErrorOverride:

说明: 用于在代理错误时显示自定义错误页面,而不是默认的错误页面。
示例:

ProxyErrorOverride On
  • ProxyVia:

说明: 向后端服务器发送 Via 头,其中包含 Apache 的版本信息。
示例:

ProxyVia On
  • ProxyBadHeader:

说明: 用于阻止包含无效字符的请求头传递给后端服务器。

ProxyBadHeader Error 400
  • BalancerMember:

说明: 用于定义负载均衡集群中的成员。
示例:

<Proxy balancer://mycluster>
BalancerMember http://backend1.example.com
BalancerMember http://backend2.example.com
</Proxy>
  • SSLProxyEngine:

说明: 用于启用或禁用 SSL 代理功能。
当你在 Apache 中设置反向代理,并且需要代理 HTTPS 请求时,就需要启用 SSLProxyEngine。这个指令告诉 Apache 在代理服务器和目标服务器之间使用 SSL/TLS 加密,从而确保数据在两者之间传输时的安全性。

示例:

SSLProxyEngine On|Off
  • SSLEngine:

当你希望 Apache 提供 HTTPS 服务时,需要使用 SSLEngine 指令来启用 SSL/TLS 支持。这通常与 SSLCertificateFile、SSLCertificateKeyFile 等其他指令一起使用,以便正确配置服务器的证书和私钥。

示例:

SSLEngine on|Off

实际应用场景

场景 1:API 网关
  • 将客户端的 API 请求转发到后端服务。

  • 示例:

    ProxyPass /api/ https://backend-api.example.com/
    ProxyPassReverse /api/ https://backend-api.example.com/
    
场景 2:负载均衡
  • 将请求分发到多个后端服务器。

  • 示例:

    ProxyPass /api/ balancer://mycluster/
    ProxyPassReverse /api/ balancer://mycluster/
    
    <Proxy balancer://mycluster>
        BalancerMember https://backend1.example.com
        BalancerMember https://backend2.example.com
    </Proxy>
    
场景 3:HTTPS 穿透
  • 当前端和后端都使用 HTTPS 时,确保通信安全。

  • 示例:

    SSLProxyEngine on
    ProxyPass /api/ https://secure-backend.example.com/
    ProxyPassReverse /api/ https://secure-backend.example.com/
    

实际例子

# robot  - 将rebot.genekang.com 请求转发到5000端口
<VirtualHost *:443>
    DocumentRoot "/var/www/html/robot"
    ServerName robot.genekang.com:443
    Header set Access-Control-Allow-Origin "http://120.25.0.137"

    ErrorLog "logs/robot.genekang.com-error_log"
    CustomLog "logs/robot.genekang.com-access_log" common
    LogLevel warn

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    SSLCertificateFile /etc/httpd/conf/genekang-ssl/genekang.com.cer
    SSLCertificateKeyFile /etc/httpd/conf/genekang-ssl/genekang.com.key
    SSLCertificateChainFile /etc/httpd/conf/genekang-ssl/fullchain.cer

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

    CustomLog logs/ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
    ProxyPreserveHost Off
    ProxyRequests Off
    ProxyPass /api/ http://127.0.0.1:5000/
    ProxyPassReverse /api/ http://127.0.0.1:5000/

</VirtualHost>


# report - 将ar-api.genekang.com 请求转发到本地8080端口
<VirtualHost *:443>
    DocumentRoot "/mnt/wxapp/report"
    ServerName ar-api.genekang.com:443
    Header set Access-Control-Allow-Origin "*"

    ErrorLog "logs/ar-api.genekang.com-error_log"
    CustomLog "logs/ar-api.genekang.com-access_log" common
    LogLevel warn

    SSLEngine on

    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    SSLCertificateFile /etc/httpd/conf/genekang-ssl/genekang.com.cer
    SSLCertificateKeyFile /etc/httpd/conf/genekang-ssl/genekang.com.key
    SSLCertificateChainFile /etc/httpd/conf/genekang-ssl/fullchain.cer

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0
    CustomLog logs/ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/

</VirtualHost>


参考地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ifanatic

觉得对您有用,可以友情打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值