[英]ProxyPass & ProxyPassReverse - Get original URL from browser address bar
I have created two websites( http://localhost/webone , http://localhost/webtwo ). 我创建了两个网站( http:// localhost / webone , http:// localhost / webtwo )。
Web two has URL like this: http://localhost/webtwo/webone (Use ProxyPass and ProxyPassReverse) 第二个网站具有这样的URL: http:// localhost / webtwo / webone (使用ProxyPass和ProxyPassReverse)
If we go to above URL, that display contents of the web one . 如果我们转到上面的URL,那将显示Web的内容。
Now If someone access web one, then I want to catch user access URL.(It could be http://localhost/webone or http://localhost/webtwo/webone ) 现在,如果有人访问网络,则我想捕获用户访问URL。(可以是http:// localhost / webone或http:// localhost / webtwo / webone )
My Issue is: 我的问题是:
If someone access web two from http://localhost/webtwo/webone URL. 如果有人从http:// localhost / webtwo / webone URL访问网站2。 Then if I execute following code it returns http://localhost/webone .
然后,如果我执行以下代码,它将返回http:// localhost / webone 。
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But browser display URL is http://localhost/webtwo/webone . 但是浏览器的显示URL是http:// localhost / webtwo / webone 。 Can someone please suggest a way to catch http://localhost/webtwo/webone URL.
有人可以建议一种捕获http:// localhost / webtwo / webone URL的方法。
When you are behind a Proxy : 当您位于代理之后 :
Use $_SERVER['HTTP_X_FORWARDED_FOR']
in place of $_SERVER['REMOTE_ADDR']
使用
$_SERVER['HTTP_X_FORWARDED_FOR']
代替$_SERVER['REMOTE_ADDR']
Use $_SERVER['HTTP_X_FORWARDED_HOST']
and $_SERVER['HTTP_X_FORWARDED_SERVER']
使用
$_SERVER['HTTP_X_FORWARDED_HOST']
和$_SERVER['HTTP_X_FORWARDED_SERVER']
in place of $_SERVER['SERVER_NAME']
代替
$_SERVER['SERVER_NAME']
http://php.net/manual/de/reserved.variables.server.php http://php.net/manual/de/reserved.variables.server.php
You can add your custom request header by using RequestHeader directive to request headers list (right after ProxyPass*
): 您可以使用RequestHeader指令来添加自定义请求标头,以请求标头列表(在
ProxyPass*
):
RequestHeader add X-REQUEST-URI "expr=%{HTTP_HOST}%{REQUEST_URI}"
This way you will have a header named HTTP_X_REQUEST_URI
, a holder of requested URI and is accessible via $_SERVER['HTTP_X_REQUEST_URI']
: 这样,您将获得一个名为
HTTP_X_REQUEST_URI
的标头,该标头是请求的URI的持有者,可以通过$_SERVER['HTTP_X_REQUEST_URI']
:
'HTTP_X_REQUEST_URI' => string 'localhost/webtwo/webone' (length=23)
Also there are some headers which are set by mod_proxy
and you may find useful. 还有一些由
mod_proxy
设置的标头,您可能会发现有用。 From apache.org : 来自apache.org :
When acting in a reverse-proxy mode (using the
ProxyPass
directive, for example),mod_proxy_http
adds several request headers in order to pass information to the origin server.当在反向代理模式下工作时(例如,使用
ProxyPass
指令),mod_proxy_http
添加多个请求标头,以便将信息传递到原始服务器。 These headers are:这些头是:
X-Forwarded-For The IP address of the client.
X-Forwarded-For客户端的IP地址。
X-Forwarded-Host The original host requested by the client in the Host HTTP request header.
X-Forwarded-Host客户端在Host HTTP请求标头中请求的原始主机。
X-Forwarded-Server The hostname of the proxy server.
X-Forwarded-Server代理服务器的主机名。
ProxyPass adds X-Forwarded-Host
header, which contains original host, and is accessible in php as $_SERVER['HTTP_X_FORWARDED_HOST']
. ProxyPass添加
X-Forwarded-Host
标头,其中包含原始主机,并且可以在php中通过$_SERVER['HTTP_X_FORWARDED_HOST']
。
Your code can be something like this: 您的代码可以是这样的:
$host = $_SERVER['HTTP_HOST'];
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
$actual_link = "http://" . $host . $_SERVER['REQUEST_URI'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.