Apache2配置不会将端口80上的所有请求重定向到443

时间:2017-04-25 14:58:03

标签: ubuntu curl apache2 url-redirection

我在我的网络服务器上配置了apache2:

Server version: Apache/2.4.18 (Ubuntu)
Server built:   2016-07-14T12:32:26

我尝试将所有HTTP流量重定向到HTTPS:

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
    ...
</VirtualHost>

我的配置工作正常,但是,我遇到了以下问题:

> curl -IL "http://example.com/"

HTTP/1.1 301 Moved Permanently
Date: Tue, 25 Apr 2017 14:44:25 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://www.example.com/
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 200 OK
Date: Tue, 25 Apr 2017 14:44:25 GMT
Keep-Alive: timeout=10
Connection: Keep-Alive
Accept-Ranges: bytes
Cache-Control: must-revalidate, max-age=604800
Content-Length: 226
Content-Type: text/html
X-HW: 1493131465.dop004.la2.t,1493131465.cds052.la2.shn,1493131465.dop004.la2.t,1493131465.cds025.la2.c
Last-Modified: Thu, 16 Feb 2017 17:05:44 GMT
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
GEO: US
ETag: "e2-548a8cec41423"
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

> curl -IL "http://www.example.com/"

HTTP/1.1 200 OK
Date: Tue, 25 Apr 2017 14:42:57 GMT
Keep-Alive: timeout=10
Connection: Keep-Alive
Accept-Ranges: bytes
Cache-Control: must-revalidate, max-age=604800
Content-Length: 226
Content-Type: text/html
X-HW: 1493131377.dop006.la2.t,1493131377.cds025.la2.c
Last-Modified: Thu, 16 Feb 2017 17:05:44 GMT
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
GEO: US
ETag: "e2-548a8cec41423"
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

正如您在上面的cURL中所看到的,我的网络服务器正在将非www HTTP流量重定向到www HTTPS,但是,当我尝试从www HTTP到www HTTPS的相同重定向时,它会失败,并在我应该显示200时得到301。

此时的任何建议都将非常感谢。

1 个答案:

答案 0 :(得分:0)

原来问题是由于我的CDN。我之后做了相应的政策更改,我的网络服务器重定向现在正在运行。