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

在这里插入图片描述

反向代理逻辑类似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 [email protected]
    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
  • 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 代理功能。
示例:

SSLProxyEngine On|Off

参考地址

猜你喜欢

转载自blog.csdn.net/hmx224_2014/article/details/135286381