Root Apache virtualhost, www dan pengaturan sub-domain termasuk http ke https

Jan 21 2020

Saya sangat terjebak. Akan sangat menghargai bantuan.

Dua situs web (wordpress dan alat php lainnya) pada instans AWS EC2 Ubuntu 18.04 yang menjalankan tumpukan LAMP.

Tujuan saya adalah untuk mencapai:

  • http://www.example.com
  • http://example.com
  • https://www.example.com

ke: https://example.com.

DAN

  • http://app.example.com

ke: https://app.example.com.

Saya telah mengkonfigurasi virtualhost dan bekerja seperti yang saya inginkan, saya kemudian membuat sertifikat https melalui letsencrypt yang mengatur pengalihan HTTP ke HTTPS ... namun ... ada yang salah:

HTTP semua dialihkan ke HTTPS.

https://app.example.com memutuskan ke situs web yang dimaksudkan - situs web1.

https://example.comtidak menyelesaikan ke situs2 - ini memutuskan ke situs1 - perhatikan itu tidak mengalihkan ke aplikasi. tetapi menampilkan situs web1 pada https://example.com.

Saya punya dua file conf virtualhost, satu untuk setiap situs. Keduanya berisi konfigurasi situs 80 dan 443 masing-masing, di bawah ini.

Sertifikat SSL memiliki nama umum example.com dan mencantumkan nama alternatif untuk app.example.com, www.example.comdan example.com.

DNS memiliki example.com Ake IP server, www.dan app.are CNAMEke example.com.

app.example.com.conf - Situs web 1

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/website1/
ServerName example.com
ServerAlias app.example.com
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app.example.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /var/www/website1
ServerName example.com
ServerAlias app.example.com
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

example.com.conf - Situs web 2

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

Saya sangat menghargai bantuan, saran, dan ide lainnya!

Jawaban

1 MrWhite Jan 22 2020 at 02:01

https://example.comtidak menyelesaikan ke situs2 - ini memutuskan ke situs1 - perhatikan itu tidak mengalihkan ke aplikasi. tetapi menampilkan situs web1 padahttps://example.com

Ya, karena semua vHost Anda ditetapkan example.comsebagai ServerNamedan saya berharap website1 muncul pertama kali dalam konfigurasi.

Untuk app.example.com.conf (Situs web 1) Anda harus menyetel ServerName app.example.comdan menghapus ServerAliasperintah sekaligus untuk penampung vhost: 80 dan vhost: 443.

Pengalihan HTTP ke HTTPS di penampung vhost: 80:

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

Dapat disederhanakan menjadi satu mod_alias Redirectdirektif:

Redirect 301 / https://app.example.com/

Misalnya example.com.conf (Situs web 2) Anda telah menduplikasi wadah vhost: 80 dan vhost: 443 dan mendefinisikan yang sama ServerName example.comdi masing-masing - ini adalah kesalahan. Sangat mungkin untuk memiliki 4 kontainer seperti ini (dan menggunakan Redirectdirektif mod_alias ), tetapi Anda akan membutuhkan ServerNamearahan unik untuk setiap vHost dan mengurangi pengulangan.

Pendekatan termudah dari sudut pandang Anda mungkin hanya dengan menghapus wadah kedua (duplikat) vhost: 80 dan vhost: 443 dan mengatur yang berikut di kedua wadah vhost: 80 dan vhost: 443 yang tersisa.

ServerName example.com
ServerAlias www.example.com

Saya kemudian membuat sertifikat https melalui letsencrypt yang mengatur pengalihan HTTP ke HTTPS

Membuat sertifikat SSL menggunakan LetsEncrypt seharusnya tidak "mengatur pengalihan HTTP ke HTTPS"?