向目录添加尾部斜杠时,Apache 从 HTTPS 重定向到 HTTP

Adr*_*ith 5 amazon-elb apache-2.4

我在 AWS 上使用 Docker 使用 Apache。Apache 侦听端口 80 并提供 HTTP 服务。

Apache 位于 AWS ELB 负载均衡器之后,该负载均衡器仅侦听为 HTTPS 提供服务的端口 443。

当我请求https://example.com/foo/(带有斜杠)时它工作正常,我的内容被提供。

当我请求https://example.com/foo(没有尾部斜杠)时,它会重定向到http://example.com/foo/ - 也就是说它添加了尾部斜杠(正确)但从 HTTPS 重定向到 HTTP(不正确)。

我该怎么办?

我的 Dockerfile 是

FROM httpd:2.4
COPY . /usr/local/apache2/htdocs/
Run Code Online (Sandbox Code Playgroud)

当我通过 curl 执行请求时:

$ curl -v https://example.com/foo
*   Trying 1.2.3.4...
* TCP_NODELAY set
* Connected to example.com (1.2.3.4) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: example.com
* Server certificate: Amazon
* Server certificate: Amazon Root CA 1
* Server certificate: Starfield Services Root Certificate Authority - G2
> GET /foo HTTP/1.1
> Host: example.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Tue, 28 Nov 2017 09:17:26 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 247
< Connection: keep-alive
< Server: Apache/2.4.29 (Unix)
< Location: http://example.com/foo/
< 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com/foo/">here</a>.</p>
</body></html>
* Connection #0 to host example.com left intact
Run Code Online (Sandbox Code Playgroud)

提前致谢。

小智 5

在 httpd.conf 中,将 ServerName 从 localhost 更改为

ServerName https://localhost
Run Code Online (Sandbox Code Playgroud)

在此参数的开头添加 https:// 解决了我完全相同的问题。


Esa*_*nen 3

< Server: Apache/2.4.29 (Unix)\n< Location: http://example.com/foo/\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是您的后端 Apache 的发言。它不知道您的客户端正在使用 HTTPS,并在独立工作时进行重定向。如果不提供任何配置,就很难知道重定向是如何完成的,但您必须修改它以重定向到https://, 。

\n\n

AWS 知识中心的这个(不是而是)解决方案可以指导您向前迈出一步。虽然最初的问题有点不同,但提供的解决方案可能也适用于这种情况。

\n\n
\n

如何将服务器上的 HTTP 流量重定向到负载均衡器上的 HTTPS?

\n\n

解决

\n\n

使用X-Forwarded-ProtoHTTP 请求的标头,更改您的\n Web 服务器\xe2\x80\x99s 重写规则,使其仅在客户端协议为\n HTTP 时应用。忽略客户端使用的所有其他协议的重写规则。\n

\n\n

这样,如果客户端使用 HTTP 访问您的网站,它们将被重定向到 HTTPS URL;如果客户端使用 HTTPS,它们将直接由 Web 服务器提供服务。

\n\n

阿帕奇

\n\n
<VirtualHost *:80>\n...\nRewriteEngine On\nRewriteCond %{HTTP:X-Forwarded-Proto} =http\nRewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]\n...\n</VirtualHost>\n
Run Code Online (Sandbox Code Playgroud)\n
\n