大网数据广告活动
服务器运维问答

How do you force an Apache Load balancer to rewrite http 302 redirects to https?

· 52CE 我爱测

I've got a website that I want to always be served in https. The site is load balanced using Apache. The Apache load balancer is configured basically like so: <Proxy balancer://mycluster> BalancerMember http://server1 route=serve…

问题原文

原帖链接 · Brad Parks · CC BY-SA 3.0

I've got a website that I want to always be served in https. The site is load balanced using Apache.

The Apache load balancer is configured basically like so:

<Proxy balancer://mycluster>
   BalancerMember http://server1 route=server1
   BalancerMember http://server2 route=server2
</Proxy>

Note that all http requests are being rewritten to https requests fine using mod rewrite as well.

The thing I'd like to do is ensure that any http 302 Location headers from server 1 or server 2 are rewritten and sent as https 302 redirect headers.

e.g.

If a response from server 1 had the following as a header:

Location: http://server1/test

I'd like it to be rewritten securely as

Location: https://server1/test

This would avoid the request being sent to the client, and the client then sending the http request, which gets rewritten to https, and would also avoid any security issues of responses being sent over http.

How can I do this?

已采纳解答

原帖链接 · Cameron Kerr · CC BY-SA 3.0

You can't do this with mod_rewrite, as that is only for requests.

Further, operating on Location is not a 100% solution (that it would be pretty high), as it won't capture things like HTTP meta-refresh (not very common these days...), or instances where the URL is generated using client-side solutions (eg. Javascript, embedded content).

Thus, the desire you propose, while useful, must be treated as an optimisation only.

There could also be sensitivities at the application-end of things. Without knowing anything your application, here are some potential things-to-consider:

  • cookie generation and the use of things like the 'secure' attribute
  • the application might have generated the URL and done some sort of crypto-signing (perhaps for SSO or authentication purposes, and rewriting it to https may invalidate the URL).... unlikely in practice, but certainly possible.

You could perhaps do this with mod_header though, which can manipulate response headers. See http://httpd.apache.org/docs/2.2/mod/mod_headers.html

Header edit Location ^http: https:

I haven't tested that at all, note that a condition can be specified before the edit keyword.

Hope it helps, Cameron

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-09-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。