F
F
fdroid2017-09-01 12:57:22
Apache HTTP Server
fdroid, 2017-09-01 12:57:22

How to set up a redirect to https when using mod_proxy?

Hello.

Given:
1.Server under Ubuntu Server 16.04, LAMP installed, site1.ru working
2.Domain name site2.ru
3.NAS Synology in the same local network with the server.

Required:
1. Organize access to the NAS web interface via the site2.ru domain
2. Site1.ru and site2.ru must work via https, with an automatic redirect from http to https.

The first question was solved using mod_proxy, there is access to the NAS, the site on the server also works. But only if you explicitly set http s ://site1.ru and http s ://site2.ru. If you enter just site1.ru and site2.ru in the browser line, then the browser displays the following:

Bad Request
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.


In site1.ru I tried to set up a redirect through htaccess, used a variety of googled combinations, none of them worked. mod_rewrite is enabled.

But what to do with site2.ru, which, in fact, just redirects to another machine, is completely incomprehensible - here it is not clear where to apply htaccess, because, of course, there is simply no site2 in /var/www, and the virtual host is simply describes proxying. The following virtual hosts are included:

site1.ru

<IfModule mod_ssl.c>
   <VirtualHost _default_:443>

     ServerAdmin [email protected]
     ServerName site1.ru
     DocumentRoot /var/www/site1

     <Directory /var/www/site1/>
       Options +FollowSymlinks
       AllowOverride All

      <IfModule mod_dav.c>
        Dav off
      </IfModule>

       SetEnv HOME /var/www/site1
       SetEnv HTTP_HOME /var/www/site1
     </Directory>

     <IfModule mod_headers.c>
          Header always set Strict-Transport-Security "max-age=15768000; preload"
     </IfModule>

     SSLEngine on
     SSLCertificateFile /etc/ssl/site1/domain_name.crt
     SSLCertificateKeyFile /etc/ssl/site1/private.key
     SSLCertificateChainFile /etc/ssl/site1/chain.crt
     Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
   </VirtualHost>
</IfModule>


site2.ru
<VirtualHost *:80 *:443>
  ServerAdmin [email protected]
  DocumentRoot /var/www/html
  ServerName site2.ru
  ServerAlias www.site2.ru
  SSLEngine On
  SSLProxyEngine On
  SSLCertificateFile /etc/ssl/site2/domain_name.crt
  SSLCertificateKeyFile /etc/ssl/site2/private.key
  SSLCertificateChainFile /etc/ssl/site2/chain.crt
  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia full
  <proxy *>
    Order deny,allow
    Allow from all
  </proxy>
  ProxyPass / http://192.168.0.102:5000/
  ProxyPassReverse / http://192.168.0.102:5000/
</VirtualHost>


How can I organize a redirect to https without using htaccess in my case? I will be glad to indicate errors in my configs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fdroid, 2017-09-01
@fdroid

Set it up!)
1. I had 000-default disabled. First, I brought it to mind:

<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

and enabled:
After that, site1.ru began to redirect to https. The redirect did not work on site2.ru.
In the virtual host config for site2.ru, after the SSL settings, I added the same code as for 000-default and brought it to the form:
<VirtualHost *:80 *:443>
  ServerAdmin [email protected]
  DocumentRoot /var/www/html
  ServerName site2.ru
  ServerAlias www.site2.ru
  SSLEngine On
  SSLProxyEngine On
  SSLCertificateFile /etc/ssl/site2/domain_name.crt
  SSLCertificateKeyFile /etc/ssl/site2/private.key
  SSLCertificateChainFile /etc/ssl/site2/chain.crt
  RewriteEngine On                                                                
  RewriteCond %{HTTPS} off                                                  
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}  
  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia full
  <proxy *>
    Order deny,allow
    Allow from all
  </proxy>
  ProxyPass / http://192.168.0.102:5000/
  ProxyPassReverse / http://192.168.0.102:5000/
</VirtualHost>

After that, the https redirect worked for site2.ru as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question