简体   繁体   English

具有动态主机名的Apache ProxyPass

[英]Apache ProxyPass with dynamic hostname

I'm trying to use Apache as a gateway to reverse proxy to a backend server with the same name as the requested http_host. 我正在尝试使用Apache作为网关,以反向代理到与请求的http_host同名的后端服务器。

ex: 例如:

    ProxyPass            /  https://%{HTTP_HOST}/
    ProxyPassReverse     /  https://%{HTTP_HOST}/

I'm getting an error when I use this setup. 使用此设置时出现错误。 Suggestions? 建议?

To use Apache ProxyPass directives with dynamic hostnames you will need to also use ModRewrite. 要将Apache ProxyPass指令与动态主机名一起使用,您还需要使用ModRewrite。

Objective 目的

All requests to the virtualhost will ProxyPass and ProxyPassReverse (also known as an "Apache Gateway") to the %{HTTP_HOST} 对虚拟主机的所有请求都将对%{HTTP_HOST}的ProxyPass和ProxyPassReverse(也称为“ Apache网关”)

The only reason this would make sense to do is if you have localhost entries on the apache server for specfic host names 这样做有意义的唯一原因是,如果您在apache服务器上具有本地主机条目以获取特定的主机名

Examples 例子

Localhost File Localhost文件

10.0.0.2 foo.bar.com    
10.0.0.3 bar.bar.com    

How it works 这个怎么运作

  1. The client makes a request to foo.bar.com (dnslookup is a public IP... YOUR APACHE SERVER) 客户端向foo.bar.com发出请求(dnslookup是公共IP ...您的APACHE服务器)
  2. Your apache server has a localhost entry of 10.0.0.2 for foo.bar.com (some other server on your network) 您的apache服务器的foo.bar.com的本地主机条目是10.0.0.2(网络上的其他服务器)
  3. The request goes through ModRewrite and /path1 is appended, then handed off to ProxyPass and ProxyPassReverse 该请求通过ModRewrite进行并附加/ path1,然后移交给ProxyPass和ProxyPassReverse
  4. ProxyPass and ProxyPassReverse hand the call off to foo.bar.com at ip 10.0.0.2 ProxyPass和ProxyPassReverse将呼叫移至IP 10.0.0.2处的foo.bar.com。

Client requests foo.bar.com ---reverse proxies to----> foo.bar.com/path1 (on some OTHER internal server) 客户端请求foo.bar.com-反向代理-> foo.bar.com/path1(在其他内部服务器上)

Apache Configuration Apache配置

    <VirtualHost *:443>
    Servername *

    # Must not contain /path1 in path (will add /path1)
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/path1/.*
    RewriteRule ^/(.*) https://%{HTTP_HOST}/path1$1 [NC,R=302,L]

    # Must contain /path1 in path (will send request to the proxy)
    RewriteEngine On
    RewriteOptions Inherit
    RewriteCond %{REQUEST_URI} ^/path1/.*
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [NC,P]

    SSLEngine on
    SSLProxyEngine On
    ProxyRequests Off

    ProxyPass            /  https://$1/
    ProxyPassReverse     /  https://$1/

    ProxyPreserveHost On

    ###################
    # SSL Constraints #
    ###################

    SSLProtocol -ALL +SSLv3 +TLSv1

    # Choose cipher suites
    SSLHonorCipherOrder On
    SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:!LOW:!SSLv2:!EXPORT

    # SameOrigin The page can only be displayed in a frame on the same origin as the page itself
    Header set X-Frame-Options SAMEORIGIN

    SSLCertificateFile     /etc/apache2/example.crt
    SSLCertificateKeyFile  /etc/apache2/example.key
    SSLCertificateChainFile /etc/apache2/gd_bundle.crt
    SetOutputFilter INFLATE;proxy-html;DEFLATE
</VirtualHost>

source: http://brakertech.com/apache-proxypass-with-dynamic-hostname/ 来源: http//brakertech.com/apache-proxypass-with-dynamic-hostname/

There's no way to dynamically reverse proxy like that using proxy pass. 没有办法像使用代理传递那样动态地反向代理。 However, you can do it using mod_rewrite's P flag. 但是,您可以使用mod_rewrite的P标志来实现。 The same thing with ProxyPassReverse , you can't use the %{HTTP_HOST} , however , since the hostnames are the same as the same, you don't need it at all. ProxyPassReverse相同的是,您不能使用%{HTTP_HOST}但是 ,由于主机名相同,因此根本不需要它。 Just need: 只需要:

RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [L,P]

One issue you may run into is that since DNS resolves proxying server to some IP, the proxying server must know that the same DNS hostname does not resolve to itself and actually resolves to a backend server (the server to proxy to ), otherwise it will cause a loop. 你可能会遇到的一个问题是,由于DNS解析代理服务器的一些IP,该代理服务器必须知道,同样的DNS主机名解决自身和实际解析到后端服务器(服务器代理 ),否则会造成循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM