Redirect Apache application to dot net core application - apache

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>

Related

Apache and Spring https redirect to wrong URL

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>

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.

Apache2 VirtualHost SSL config issues

Debian 10 / Apache2 -
GOAL: redirect all web traffic to VPS server to SSL. I have 2 sites hosted: bjmurrey.com and nextcloud.bjmurrey.com. I have 1 IP address.
PROBLEM: I can't for the life of me find a way to make this work. I have tried 1000 suggestions posted here and elsewhere with no success. The closest I get is everything redirects to https://bjmurrey.com when typing in nextcloud.bjmurrey.com. I am also a novice at this so please be kind.
I have a default.conf that looks like this:
<VirtualHost *:80>
ServerName www.bjmurrey.com
ServerAlias bjmurrey.com
DocumentRoot /var/www/blog
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://bjmurrey.com/
</VirtualHost>
<VirtualHost *:80>
ServerName nextcloud.bjmurrey.com
DocumentRoot /var/www/nextcloud
ServerAlias nextcloud.bjmurrey.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://nextcloud.bjmurrey.com/
</VirtualHost>
I have a default-ssl.conf that looks like this:
<VirtualHost bjmurrey.com:443>
ServerName bjmurrey.com
ServerAlias www.bjmurrey.com
DocumentRoot /var/www/blog
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/bjmurrey.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/bjmurrey.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
<VirtualHost nextcloud.bjmurrey.com:443>
ServerName nextcloud.bjmurrey.com
DocumentRoot /var/www/nextcloud
ServerAlias nextcloud.bjmurrey.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/nextcloud.bjmurrey.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/nextcloud.bjmurrey.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
bjmurrey.com is in var/www/blog and nextcloud.bjmurrey.com in the var/www/nextcloud directories.
I have my DNS records www and nextcloud both pointed to same IP. I only have 1 IP available. This is a very low traffic site.
I have done all kinds of things like just have the default virtual host redirect to only nextcloud and removed all mention of the www site and it still redirects to bjmurrey.com, or redirects with an SSL error saying that nextcloud can't be loaded because the ssl is for bjmurrey.com only. As you can see I have made certs with letsencrypt for both bjmurrey.com and nextcloud.bjmurrey.com.
I'm about to yank all my hair out in frustration, so I know I've tapped out all my knowledge here. Help!
I have retested with http and is working for me.
Both addresses mydomain.myhost.example.com and myhost.example.com resolve to the same IP.
This is my configuration file
ServerName example.com
<VirtualHost *:80>
ServerName mydomain.myhost.example.com
DocumentRoot /var/www/mydomain
</VirtualHost>
<VirtualHost *:80>
ServerName myhost.example.com
DocumentRoot /var/www/myhost
</VirtualHost>
Tested in Ubuntu 20.04.1, Apache 2.4

Reverse Proxy works but not Virtual Host in the secondary RPi Web Server

I have two RPi(3+), both use for LAMP web server. And their relation is like this image
So, Rpi1 is the one directly talks to my home router and I set up reverse proxy to retrieve RPi2 Data.
My problem is I was able to access both RPi1 sites with their domains (static1.com and wordpress1.com) but both of RPi2 domains(static2.com and wordpress2.com) lead to the same site(wordpress2.com).
I assume my virtual host setting in RPi2 has some problem but could not find any clue yet... I need help solving this apache setting issue!
Here is my set up in RPi 1 Apache server,
static2.com setting:
pi#cloelee:/etc/apache2/sites-available $ cat static2.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName static2.com
ServerAlias www.static2.com
SSLProxyEngine on
ProxyPass / http://192.168.1.18/
ProxyPassReverse / http://192.168.1.18/
ProxyPreserveHost Off
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#example.com
ServerName static2.com
ServerAlias www.static2.com
SSLProxyEngine on
ProxyPass / http://192.168.1.18/
ProxyPassReverse / http://192.168.1.18/
ProxyPreserveHost Off
SSLCertificateFile /etc/letsencrypt/live/static2.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/static2.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
wordpress2.com setting:
pi#cloelee:/etc/apache2/sites-available $ cat wordpress2.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName wordpress2.com
ProxyPass / http://192.168.1.18/
ProxyPassReverse / http://192.168.1.18/
ProxyPreserveHost Off
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#example.com
ServerName wordpress2
ProxyPass / http://192.168.1.18/
ProxyPassReverse / http://192.168.1.18/
ProxyPreserveHost Off
</VirtualHost>
And this is my setup in RPi2 Apache Server,
static2.com setting:
pi#kyongoh:/etc/apache2/sites-available $ cat static2.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName static2.com
ServerAlias www.static2.com
DocumentRoot /var/www/static2.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?static2\.com$
RewriteRule (.*) https://static2.com/$1 [L,R,NE]
</VirtualHost>
pi#kyongoh:/etc/apache2/sites-available $ cat static2.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName static2.com
ServerAlias www.static2.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/static2.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/static2.com/privkey.pem
</VirtualHost>
</IfModule>
wordpress2.com setting:
pi#kyongoh:/etc/apache2/sites-available $ cat wordpress2.com.conf
<VirtualHost *:80>
<Directory "/var/www/wordpress2.com">
AllowOverride All
</Directory>
ServerName wordpress2.com
ServerAdmin webmaster#local_wordpress
DocumentRoot /var/www/wordpress2.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#example.com
ServerName wordpress2.com
ServerAdmin webmaster#local_wordpress
DocumentRoot /var/www/wordpress2.com
</VirtualHost>
As you may realize wordpress2.com has not SSL yet but static2.com has. Do you see why both RP2 domains request wordpress2 data? what did I miss?
Thanks for help!
It was simple fix, just missed to turn ProxyPreserveHost on
ProxyPreserveHost Off
to
ProxyPreserveHost On

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.