将HTTP重定向到https仅在刷新页面Apache2后有效

时间:2018-09-29 00:03:02

标签: apache redirect https subdomain virtualhost

我已经在我的网站和example.com上安装了SSL证书,一切正常,这意味着键入example.com可以正确重定向到https://example.com。但是,我还为子域安装了一个证书,因此链接变为:subdomain.example.com。

我的目标是将subdomain.example.com重定向到https://subdomain.example.com。这听起来可能很奇怪,但这是半成功的,这意味着当我第一次浏览subdomain.example.com时,它使用的是http协议,但是当我刷新同一页面时,它会切换为https协议。

这是我的VirtualHost conf文件(端口80):

<VirtualHost *:80>
  ServerName subdomain.example.com
  ServerSignature Off

  ProxyPreserveHost On

  AllowEncodedSlashes NoDecode

  <Location />
    Require all granted

    ProxyPassReverse http://127.0.0.1:8181
    ProxyPassReverse http://example.com/
  </Location>

  RewriteEngine on

  #Forward all requests to gitlab-workhorse except existing files like error documents
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

  # needed for downloading attachments
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

RewriteCond %{SERVER_NAME} =subdomain.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/ [R=301,L]

</VirtualHost>

我已从上面的示例中删除了不相关的行。这是443 conf文件:

< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
<    IfModule mod_ssl.c>
    SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
    <VirtualHost *:443>
      ServerName subdomain.example.com
      ServerSignature Off

      ProxyPreserveHost On

      AllowEncodedSlashes NoDecode

      <Location />
        Require all granted

        #Allow forwarding to gitlab-workhorse
        ProxyPassReverse http://127.0.0.1:8181
        ProxyPassReverse http://domain/
      </Location>

      RewriteEngine on

      #Forward all requests to gitlab-workhorse except existing files like error documents
      RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
      RewriteCond %{REQUEST_URI} ^/uploads/.*
      RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

      # needed for downloading attachments
      DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public


    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/subexample.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    Header always set Strict-Transport-Security "max-age=31536000"
    SSLUseStapling on
    Header always set Content-Security-Policy upgrade-insecure-requests
    </VirtualHost>
    </IfModule>

值得注意的是我正在使用certbot。

希望有人能帮助我。

1 个答案:

答案 0 :(得分:0)

您说“我的目标是将subdomain.example.com重定向到https://subdomain.example.com

那为什么在您的:80 VirtualHost中拥有所有这些代理配置?只需将重定向强制为:443,然后让:443处理代理(和其他代理)即可。

因此您的VirtualHost将变为:

<VirtualHost *:80>
    ServerName subdomain.example.com
    CustomLog logs/subdomain_80_access.log combined
    ErrorLog  logs/subdomain_80_error.log

    RewriteEngine On
    RedirectMatch ^/(.*)$ https://subdomain.example.com/$1

</VirtualHost>