Перенаправление http на https работает только после обновления страницы Apache2 - PullRequest
0 голосов
/ 29 сентября 2018

Я установил SSL-сертификаты на своем веб-сайте и на example.com все работает нормально, что означает, что при наборе example.com правильно перенаправляется на https://example.com. Однако я также установил сертификат для субдомена, так чтоссылка становится: subdomain.example.com.

Моя цель - перенаправить subdomain.example.com на https://subdomain.example.com.Это может звучать странно, но это полуработы, означающие, что когда я впервые захожу на subdomain.example.com, он использует протокол http, но когда я обновляю эту же страницу, он переключается на протокол https.

Это мой виртуальный Conf 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 голосов
/ 29 сентября 2018

Вы говорите "Моя цель состоит в том, чтобы 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>
...