0

I have configured my server to use httpd as proxy and Tomcat for deploying and running application. I use Apache httpd as proxy to redirect all request to port 80 and to the directory of tomcat where my web application files are located: /usr/share/tomcat/webapps/website.

The proxy lines that redirects to http://localhost:8080/website works for me, but when I tried to use HTTPS by adding additional lines like:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

then it just redirects to directory of Apache that is located in /var/www/html with default Apache httpd page.

This are my configurations in my conf.file:

<VirtualHost *:80>
    ServerName website.com
    ServerAlias www.website.com
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    DocumentRoot /usr/share/tomcat/webapps/website/
    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / http://website.com:8080/website/
    ProxyPassReverse / http://website.com:8080/website/
</VirtualHost>

<VirtualHost _default_:443>
    ErrorLog logs/ssl_error_log
    TransferLog logs/ssl_access_log
    LogLevel warn
    SSLEngine on
    SSLProtocol -all +TLSv1.3 +TLSv1.2
    SSLOpenSSLConfCmd Curves X25519:secp521r1:secp384r1:prime256v1
    SSLHonorCipherOrder on
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM
    SSLCertificateFile /etc/pki/tls/certs/domein.crt
    SSLCertificateKeyFile /etc/pki/tls/private/domein-demo.key
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>
    BrowserMatch "MSIE [2-5]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0
</VirtualHost>

The results that I get is

Forbidden
You don't have permission to access this resource.

I have already tried giving permissions to Apache to directory of Tomcat with sudo chown apache:apache /usr/share/webapps/website.

mforsetti
  • 2,666
  • 2
  • 16
  • 20
ubey
  • 1
  • 1
  • Can you clarify your question: when you access your site through HTTP (port 80) you want to: 1. redirect the browser to the HTTPS port, 2. forward the request to Tomcat **or** 3. retrieve the data in the `/usr/share/tomcat/webapps/website` directory? Your configuration performs the first operation. – Piotr P. Karwasz Apr 02 '21 at 12:08

1 Answers1

1

When you're doing this:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

You're redirecting all requests that's not HTTPS to its HTTPS counterpart, and HTTPS requests are handled by <VirtualHost _default_:443> configurations instead of <VirtualHost *:80>. Try copying or moving these configurations:

ProxyRequests off
ProxyPreserveHost On
ProxyPass / http://website.com:8080/website/
ProxyPassReverse / http://website.com:8080/website/

from <VirtualHost *:80> configuration block to <VirtualHost _default_:443>.

mforsetti
  • 2,666
  • 2
  • 16
  • 20
  • Hi mforsetti, that solved my problem, than you – ubey Apr 02 '21 at 10:33
  • just one more question with current configuration httpd redirects to my website only when i surf with WWW www.website.com so when i try to surf to http://website.com without www then i get page 404 i am guessing it can not find the the application files. – ubey Apr 02 '21 at 21:17
  • So i have reconfigure the redirect engine part RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC] RewriteCond %{https} off RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L] RewriteCond %{REQUEST_URI} !(.+)/$ RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+)$ https://www.website.com/$1/ [R=301,L] RewriteCond %{REQUEST_URI} /(.+)/$ RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ https://www.website.com/%1 [R=301,L] but this is not helping – ubey Apr 02 '21 at 21:22
  • You are using lots of conditions. Keep it simple: if you want to redirect all HTTP request to HTTPS use `RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]` or `RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]` without any conditions. – Piotr P. Karwasz Apr 03 '21 at 06:54
  • hi Piotr, i tried by disabling all conditions and using only this rules RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L] RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L] But no results i get 404 page not found but when i use www i get the whole page. – ubey Apr 03 '21 at 07:24
  • is it posibble that my certificate is not properly generated? maby i should regenerate certificate and add -alias www.website.com Second question, do i need to add or change somethink in server.xml file of tomcat /usr/share/tomcat/server.xml? the only code that i have added inside server.xml is ` ` – ubey Apr 03 '21 at 13:13