I have a Tomcat 8 app deployed on Elastic Beanstalk which uses a sub-domain of my main app. Both are separate applications and do not interact. I have a mod_rewrite
rule to redirect all http
requests to https
in a configuration file in the .ebextensions
folder -
files:
"/etc/httpd/conf.d/httpd_redirect.conf" :
mode: "000644"
owner: root
group: root
content: |
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://sub.domain.com%{REQUEST_URI} [L,R]
So ideally, someone accessing the app at sub.domain.com
or http://sub.domain.com
will be redirected to https://sub.domain.com
.
The problem is that this works only after I request the app with https
first. So, I have to request https://sub.domain.com
first, and from then onwards, non-https
requests will be redirected to https
.
Also, this only works until I clear my browser cache. Once the cache is cleared, non-https
requests are no longer redirected to https
. I have make an https
request first again for the redirection to start working.
What could be causing this? The main domain uses a separate certificate from that of the sub-domain if it matters.
How can I force the application to always use https
?
I have a secure listener enabled on the load balancer with this configuration file in .ebextensions
-
option_settings:
aws:elb:listener:443:
SSLCertificateId: arn:aws:acm:us-east-2:1234567890123:certificate/####################################
ListenerProtocol: HTTPS
InstancePort: 80
Проблема заключалась не в правиле перезаписи. Файл должен был быть помещен по определенному пути в пределах .ebextensions
, чтобы он работал в Tomcat 8. Файлы конфигурации также должны были быть настроены по-другому. Большинство предоставленных примеров не были для Tomcat, поэтому я поместил их не в то место.
Что сработало -
В /. Ebextensions / httpd / conf.d / myconf.conf
, поместите -
LoadModule rewrite_module modules/mod_rewrite.so
и в /. ebextensions / httpd / conf.d / elasticbeanstalk / 00_application.conf
, место -
<VirtualHost *:80>
<Proxy *:80>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
Обратите внимание на использование файлов .conf
вместо .config
. Это важно!
Кроме того, у меня возникло ложное ощущение перенаправления из-за того, что кеш браузера обслуживает мне сайт https
. Вот почему это не сработало, когда я очистил кеш.