Apache and Spring https redirect to wrong URL - apache

I have a SpringBoot application running on http://localhost:8080/ and Apache. Apache redirects all requests from *:80 to *:443 and 443 to http://localhost:8080.
When I try to access http://domain_name.com/login it redirects me to https://domain_name.comlogin/
Also if i try to access pages without authentication it should redirects me to login page, but it redirects me to https://domain_name.comlogin/ instead of https://domain_name.com/login and the same with /logout
What's the problem with redirecting. Is it with Apache or Spring?
<VirtualHost *:80>
ServerName domain_name.com
ServerAdmin admin#domain_name.com
Redirect permanent / https://domain_name.com/
</VirtualHost>
<VirtualHost *:443>
SSLEngine On
SSLProxyEngine On
ProxyPreserveHost On
ServerName domain_name.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/apache2/certificate/domain_name.crt
SSLCertificateKeyFile /etc/apache2/certificate/private.key
SSLCertificateChainFile /etc/apache2/certificate/chain.crt
</VirtualHost>

Related

Apache: two domains on the same server with different ports

I'm newbie on this stuff so forgive me if i'm doing a stupid question. I have a vue application running on port 80 working just fine over SSL (say www.domain.com and domain.com).
Now I need my springboot application, which is running over port 8443 to be accessible by a secure connection too (say on api.domain.com) but i can't quite figure what i'm doing wrong...
I can access the api if i'm not including the second virtualhost, but only using http... Also, when I just type api.domain.com it goes to domain.com start page too. And when I include the second virtualhost, I can't even acces domain.com.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName www.domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias domain.com
ProxyPreserveHost On
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName api.domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias api.domain.com
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8443/
ProxyPassReverse / http://127.0.0.1:8443/
</VirtualHost>
</IfModule>
Found it out: there are some apache modules that need to be activated. Just did it with
sudo a2enmod proxy
sudo a2enmod proxy_http
and everything works like a charm.

Deny all requests to a specific sub folder for virtual host

All requests to http://example.com are redirected to https and then proxy passed to my application server. So far so good.
Now I want to deny all requests to a specific sub folder, that is http://example.com/dev. How do I accomplish that?
<VirtualHost *:80>
ServerName example.com
Redirect permanent "/" "https://example.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
<Location />
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Location>
SSLEngine on
SSLCertificateFile "${SRVROOT}/conf/ssl/server.crt"
SSLCertificateKeyFile "${SRVROOT}/conf/ssl/server.key"
SSLCertificateChainFile "${SRVROOT}/conf/ssl/intermediate.crt"
</VirtualHost>

Redirect Apache application to dot net core application

I have a server which uses a domain example.com. I am using apache to run this web server. I have also installed Dot Net core and published a Dot Net core app to /var/www/app location.
I a trying to access this application using example.com/api
This below is what I have tried in 000-default.conf
<VirtualHost *:80>
ServerAdmin root#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
the below is what I hvae added for the application
<VirtualHost *:80>
ServerName example.com/api
ProxyPreserveHost On
<Proxy *>
Order allow, deny
Allow from all
</Proxy>
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
</VirtualHost>
I have also tried the below configuration.
<VirtualHost *:80>
ServerName example.com/api
redirect / http://localhost:5000/
</VirtualHost>
can someone please help me find what am I doing wrong and how to achieve this properly.
This is solved by enabling proxy and proxy_http using commands below.
a2enmod proxy
a2enmod proxy_http
Also configured the Proxy inside the virtual host as follows.
<VirtualHost *:80>
ServerAdmin root#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
#Below 3 lines worked
ProxyPreserveHost On
Proxypass "/api/" "http://localhost:5000/"
ProxyPassReverse "/api/" "http://localhost:5000/"
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache: Site become unavailable when SSL is added

I'm using Apache2 in ubuntu to rout the request to a tomcat webapp. I am also trying to add SSL into this. Below is the apache 000-default.conf file.
<VirtualHost *:80>
ProxyPreserveHost On
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
ServerName localhost
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /opt/apache-tomcat-7.0.79/webapps/myglukose_messaging_portal/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine On
# Set the path to SSL certificate
# Usage: SSLCertificateFile /path/to/cert.pem
SSLCertificateFile /etc/apache2/ssl/STAR_myglukose_com.crt
SSLCertificateKeyFile /etc/apache2/ssl/private_key.key
ProxyPreserveHost On
ProxyPass / http://<my-ip>/:8080/
ProxyPassReverse / http://<my-ip>/:8080/
ServerName localhost
</VirtualHost>
Everything was fine until I made the configuration for port 443. When that is made, the site simply becomes unavailable and the browser says "This site can’t be reached. refused to connect.". My tomcat web app is deployed as "ROOT" so can be accessed with the port 80 configuration. Whats wrong here?

Apache web server, multiple applications in different ports with on same server

I have two applications running on Jboss 6 with different context on same port (8180). I have Apache running on the machine port 80. I need to direct the request to appropriate context based on the application being accessed.
I have a dns entry - testServ14, which points to the server IP.
To be more clear, the applications should be accessible via urls something like
http://testServ14/appAcontext/
http://testServ14/appBcontext/
In httpd-vhosts file what should i be using virtualhost or namevirtualhost directives?
How can I achieve this..
Tried the following but did not work...
<VirtualHost *:80>
ServerName http://testServ14/appA
ProxyRequests Off
ProxyVia On
ProxyPass / http://localhost:8180/appA
ProxyPassReverse / http://localhost:8180/appA
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>
<VirtualHost *:80>
ServerName http://testServ14/appB
ProxyRequests Off
ProxyVia On
ProxyPass / http://localhost:8180/appB
ProxyPassReverse / http://localhost:8180/appB
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>
Thanks
-- updated
The following works nicely...you can add other apps with a different context,running on the same port.
<VirtualHost *:80>
ServerName http://us14testServ/
ServerAlias us14testServ
ProxyRequests Off
ProxyVia On
#app1
ProxyPass /app1/ http://localhost:8180/app1/
ProxyPassReverse /app1/ http://localhost:8180/app1/
#app2
ProxyPass /app2/ http://localhost:8180/app2/
ProxyPassReverse /app2/ http://localhost:8180/app2/
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>
If you want to redirect from one url to another, then what you need to use is mod_rewrite.