微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何将SCRIPT_URI写入Apache的头文件?

我有一个设置,我有这样的服务器:

负载均衡器 – > Apache – > Tomcat

我希望Apache将客户端使用的url写入头文件,所以我可以读一下,一旦我打到tomcat。

我试图使用mod_rewrite和mod_headers所以这样做,但没有运气。 如果我看http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html,那么看起来很清楚,我需要一个名为SCRIPT_URI的variables:

C ++:平台相关types – 最佳模式

我如何获得sal.h

如何使用“尾-1 * /文件名”尾尾多个文件的最后一行

请求标头字段范围不被Access-Control-Allow-Headers所允许

自相矛盾的CORS错误:没有“Access-Control-Allow-Origin”出现或者“Access-Control-Allow-Origin”头包含多个值

SCRIPT_URI=http://en1.engelschall.com/u/rse/

我也看了这个http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html所以弄清楚如何编写标题,并有一些成功,但还不够。

我有PHP安装在Apache服务器上,如果我看看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

有任何想法吗?

将Content-disposition标题仅设置到某个目录下的文件的附件中?

在Ubuntu 8.1上找不到g ++ – 4.1或其他版本的size_t

如何在Windows cmd中打印该文件的前五行

包含Windows上可能存在或不存在的标头

在winbase.h中(第97行),函数式的macrosYield()是什么?

我自己找到了解决方法,虽然这不是我想要的干净简单的解决方案。 我能够得到各个部分,并可以使用它们来构建完整的URI:

<Ifmodulee !rewrite_module> Loadmodulee rewrite_module modules/mod_rewrite.so </Ifmodulee> <Ifmodulee !headers_module> Loadmodulee headers_module modules/mod_headers.so </Ifmodulee> <Ifmodulee rewrite_module> <Ifmodulee 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" </Ifmodulee> </Ifmodulee>

根据https://serverfault.com/questions/23470/setting-apache-environment-variable-on-mod-rewrite-rewrite-condition ,你需要调整执行的顺序,通过使用不同的配置部分

在这里添加一个虚拟的<Location>部分

RewriteRule ^ - [E=FOO:set_by_rewrite_rule] RequestHeader set x-foo-outside-location %{FOO}e <Location "/bla"> RequestHeader set x-foo-in-location %{FOO}e </Location>

这些是我得到的标题

x-foo-in-location='set_by_rewrite_rule' x-foo-outside-location='(null)'

请注意,如果我使用<Location "/"> (并且我无法找到对所有请求路径有效的解决方案)

还要注意, 塞巴斯蒂安的答案是没有任何虚拟配置部分获取头中的env变量…去图!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐