我有一个设置,其中有这样的服务器:
负载均衡器 -> Apache -> Tomcat
我希望 Apache 将客户端使用的 url 写入标头中,这样一旦我点击 tomcat 就可以读取它。
我尝试使用 mod_rewrite 和 mod_headers 这样做,但没有运气。
如果我看http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html那么很明显我需要名为 SCRIPT_URI 的变量:
SCRIPT_URI=http://en1.engelschall.com/u/rse/
我也看了这个http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html所以弄清楚如何编写标题并取得了一些成功,但还不够。
我在 apache 服务器上安装了 php,如果我查看 phpinfo(),我可以看到 SCRIPT_URI 在那里并且有一个合理的值。
我只是无法将其写入标题。这是我所做的简化版本:
#load modules
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
#Get the original uri used
RewriteRule .* - [E=INFO_SCRIPT_URI:%{SCRIPT_URI},NE]
RequestHeader set x-orig-uri "%{INFO_SCRIPT_URI}e"
我已经尝试了其他几个选项,并且都在 Windows、cygwin 和 ubuntu linux 上
有任何想法吗?
我自己找到了一个解决方法,尽管这不是我想要的干净简单的解决方案。
我能够获得各个部分,并可以使用它们来构建完整的 URI:
<IfModule !rewrite_module>
LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>
<IfModule !headers_module>
LoadModule headers_module modules/mod_headers.so
</IfModule>
<IfModule rewrite_module>
<IfModule headers_module>
####### INITIAL SETUP #########################
RewriteEngine on
####### SET HEADERS #########################
#get and set the host name
RewriteRule .* - [E=INFO_HTTP_HOST:%{HTTP_HOST},NE]
RequestHeader set x-orig-host "%{INFO_HTTP_HOST}e"
#get and set the host port
RewriteRule .* - [E=INFO_SERVER_PORT:%{SERVER_PORT},NE]
RequestHeader set x-orig-port "%{INFO_SERVER_PORT}e"
#If the uri starts with a slash and some alphanumerics, then make a
#group of that until the first non-alpha (ie. the next slash)
RewriteCond %{REQUEST_URI} ^(/[\w-]+)
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_REQUEST_CONTEXT:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-context "%{INFO_REQUEST_CONTEXT}e"
#If the accept-header contains a number after ;version= then make a regex group of that number
RewriteCond %{HTTP_ACCEPT} \+json;version=(\d+)$
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_ACCEPT_VERSION:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-accept-version "%{INFO_ACCEPT_VERSION}e"
#If the accept-header contains kasia2. followed by some letters,
#then make a regex group of those letters
RewriteCond %{HTTP_ACCEPT} kasia2.(\w+).*
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_ACCEPT_NAME:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-accept-name "%{INFO_ACCEPT_NAME}e"
#If https is on ...
RewriteCond %{HTTPS} on
#...then set the protocol environment variable to "https"
RewriteRule .* - [E=INFO_PROTOCOL:https,NE]
#If https is off ...
RewriteCond %{HTTPS} off
#...then we assume it must be "http"
RewriteRule .* - [E=INFO_PROTOCOL:http,NE]
#Finally, set the protocol header
RequestHeader set x-orig-protocol "%{INFO_PROTOCOL}e"
#Get the request uri and set an environment variable
RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]
#Build the whole original url out of the available parts. SCRIPT_URI is always null, otherwise we could have used that.
RequestHeader set x-orig-url "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_URI}e"
#In addition make an url with only the host and context, for convenience
RequestHeader set x-orig-url-base "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_CONTEXT}e"
</IfModule>
</IfModule>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)