Apache Web 服务器重定向发布请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/374571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Web Server Redirect Post Request
提问by kittugadu
I configured apache on 4 ports (with virtual hosts as some ports used HTTPS) and the aim is to redirect post requests to different servlets running in a TOMCAT instance on some other machine. The calls are made from a httpclient(java client) . When I run the client it throws the error 301 Error: Moved Permanently. Need hints/tips/help on how to solve the this problem.
我在 4 个端口上配置了 apache(使用虚拟主机作为某些端口使用 HTTPS),目的是将 post 请求重定向到在其他机器上的 TOMCAT 实例中运行的不同 servlet。调用是从httpclient(java client)发出的。当我运行客户端时,它会抛出错误 301 错误:永久移动。需要有关如何解决此问题的提示/提示/帮助。
It would be nice if someone can give me a example. My virtual host (with the re-direction) looks like something like this
如果有人能给我举个例子就好了。我的虚拟主机(带有重定向)看起来像这样
<VirtualHost _default_:3334>
RewriteEngine On
RewriteRule ^/$ https://192.168.100.23:38443/Lang/Englangservices/Alapbhet_service
<\VirtualHost>
I am now able to get mod proxy working with mod rewrite and now I dont get the 302 error, but as it always happens to a newbie there is this another problem.
我现在可以让 mod 代理与 mod rewrite 一起工作,现在我没有收到 302 错误,但是因为它总是发生在新手身上,所以还有另一个问题。
HTTP POST requests are not working. When I issue the post request, the proxy fails as it goes on to look for index.htm.
HTTP POST 请求不起作用。当我发出 post 请求时,代理在继续查找 index.htm 时失败。
My httpd.conf snippet
我的 httpd.conf 片段
<VirtualHost *:3331>
ServerName localhost:3331
ProxyPass / http://192.168.100.23:38443/Lang/Englangservices/Alapbhet_service
ProxyPassReverse / http://192.168.100.23:38443/Lang/Englangservices/Alapbhet_service
RewriteEngine on
RewriteRule ^/$ http://localhost:3331/
</VirtualHost>
回答by Olaf Kock
Another option additional to the already given mod _ proxy is mod _ jk. (sorry for the spaces, otherwise formatting is messed up)
除了已经给定的 mod _ proxy 之外的另一个选项是 mod _ jk。(抱歉空格,否则格式混乱)
Both are apache extensions that allow apache to consume the request, transparently forward it to tomcat, wait for the response and then send the result back.
两者都是apache扩展,允许apache消费请求,透明转发到tomcat,等待响应,然后将结果发回。
IMHO mod_jk has a few advantages
恕我直言 mod_jk 有几个优点
- Tomcat automagically gets the served hostname, protocol and port (with mod_proxy you have to configure the fact that it's proxied in the Connector, see the options proxyName and proxyPort
- mod_jk provides loadbalancing - should you need this
- apache speaks to tomcat in a protocol designed for this task (ajp13). With mod_proxy they talk http, which has a slightly higher overhead (Disclosure: I've never measured it myself, just parrotting it)
- Tomcat 自动获取服务的主机名、协议和端口(使用 mod_proxy,您必须配置它在连接器中代理的事实,请参阅选项proxyName 和 proxyPort
- mod_jk 提供负载平衡 - 如果你需要这个
- apache 在为此任务设计的协议 (ajp13) 中与 tomcat 对话。使用 mod_proxy 他们谈论 http,它的开销略高(披露:我自己从未测量过,只是鹦鹉学舌)
What you use in the end is completely your choice - it's not thatmuch a difference between both options. (Someone correct me if it is)
你到底用什么完全是你的选择-这不是说这两个选项之间没有太大的差别。(如果是的话有人纠正我)
Configuration is as follows (untested pseudocode. Doread the docs, please understand what you do...)
配置如下(未经测试的伪代码。待办事项阅读文档,请理解你做了什么......)
# somewhere in httpd.conf, above the virtual hosts
JkWorkersFile /etc/apache2/workers.properties
JkLogFile /var/log/apache/mod_jk.log
JkLogLevel error
# your existing part with virtual hosts
<VirtualHost ...>
....
JkMount /Lang/* tomcat1
JkMount /Lang tomcat1 # if you need the directory itself also to be forwarded
....
</VirtualHost>
# the workers.properties file described above
# 'tomcat1' is the reference used above as argument to JkMount
workers.list=tomcat1
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
回答by Paul Dixon
A 301 sounds like the appropriate response if you really want to redirect, but maybe you really wanted to proxy the requests so that the client was unaware of the redirect? In this case, take a look at using mod_proxyas a reverse proxy.
如果您真的想重定向,301 听起来是合适的响应,但也许您真的想代理请求以便客户端不知道重定向?在这种情况下,看看使用mod_proxy作为反向代理。
回答by Keltia
Your httpclient in java needs to properly handle 301 answers. If it does not, it is broken. If you don't want or can't, then using mod_rewrite/mod_proxyas indicated by Paul to "transparently" redirect is the way to go. See here
您在 java 中的 httpclient 需要正确处理 301 个答案。如果没有,它就坏了。如果您不想或不能,那么使用保罗指示的mod_rewrite/ mod_proxy来“透明地”重定向是要走的路。看这里
回答by kittugadu
Achieved this using JK and mod rewrite. The problem was with two way SSL and I configured the ports to run in conjunction with 443 (SSL) port.
使用 JK 和 mod rewrite 实现了这一点。问题出在双向 SSL 上,我将端口配置为与 443 (SSL) 端口一起运行。
i.e.
IE
Note: if one intends to rewrite URL's in the virtual hosts then JK mount has to be reloaded for each virtual host.
注意:如果打算在虚拟主机中重写 URL,则必须为每个虚拟主机重新加载 JK 挂载。
My URL rewrites look like this
我的 URL 重写看起来像这样
RewriteEngine on
RewriteCond %{SERVER_PORT} 4342
RewriteRule ^/$ /Lang/Englangservices/Alapbhet_service [L,PT]
RewriteEngine on
RewriteCond %{SERVER_PORT} 4342
RewriteRule ^/$ /Lang/Englangservices/Alapbhet_service [L,PT]
Thanks everyone one for their inputs.
感谢大家的投入。

