Proxy Request if ServerName does not match - apache

I have Ubuntu 20.04 machine and in that I have installed Apache server for handling the web requests. I want to run multiple websites on my single machine.
What I want is when request is coming from specific url then apache should load the specified directory for that url. If request is not coming from that specific url then request should be proxied.
To achieve this, I tried two methods but none worked
Method One
I created two virtual host files, one for request for proxy and another for pointing to a DocumentRoot when url matches
vs1.conf
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
vs2.conf
<VirtualHost *:80>
ServerName abc.example.com
ServerAlias abc.example.com
DocumentRoot /var/www/my-website
</VirtualHost>
When I use above method, all the request points to /var/www/my-website as DocumentRoot but this should only be the case when url is abc.example.com. It is like the vs1.conf not even called and only vs2.conf file controlling the request.
Method Two
In this method, I decided to use just one virtual host file which looks like below
<VirtualHost *:80>
ServerName abc.example.com
ServerAlias abc.example.com
DocumentRoot /var/www/my-website
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
In this case, when request is coming from abc.example.com then I see a blank page as if this request is not being handled. But when request is coming from url other than abc.example.com then they are proxied correctly.
So now I am clueless about how my configuration file should look like?

Related

Apache reverse proxy leading to a blank page

I have a youtrack instance running on port 8080 of my webserver. The domain that is used to access the server is subdomain.maindomain.com
Now I want youtrack to be accessed through subdomain.maindomain.com/youtrack instead of subdomain.maindomain.com:8080.
However, when setting up my reverse proxy config, the url leads to a blank page.
My youtrack.conf looks like the following:
<VirtualHost *:80>
ServerName subdomain.maindomain.com
ProxyPreserveHost On
DocumentRoot /var/www/html
ProxyPass /youtrack/ http://subdomain.maindomain.com:8080/
ProxyPassReverse /youtrack/ http://subdomain.maindomain.com:8080/
</VirtualHost>

Apache Virtual Hosts serves the vhost NOT matched

I set up an Apache Server with two Virtual Hosts and it has a very weird behaviour. I have a normal webserver, that should be server in all cases, except given the case, that the domain name is "biblio.name" or "biblio-intra.name", which should be redirected to a virtual machine located on my laptop, serving another webservice on Linux. On my laptop I use xampp for the Apache Server. I have the following "httpd-vhosts.conf" in my apache/conf/extra folder:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName sis.name/
ServerAlias *
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
ServerName biblio.name/
ServerAlias biblio-intra.name/
</VirtualHost>
So I expected it to redirect all requests to "biblio.name" and serve the rest as normal. However, it didn't!
When I enter my static ip-adress (assigned from the router) in the browser, I get served htdocs as normal, perfectly fine. When I enter biblio.name:8080 I also get normally served the virtual machine as expected (obviously, since 8080 automatically uses the redirect rule of the virtual machine.) However, when I type "sis.name" it redirects me to the virtual machine and when I type "biblio.name" it serves me from the htdocs.
I never experienced a behaviour like that and I don't get, why it serves the opposite host of the one supposed to.
Am I missing something?
For some reason I don't know (anymore), all my apache vhost configurations have the same value for ServerName and ServerAlias. Also the / in it seems odd. You can try listing biblio-intra.name as second option, but it should at first repeat the value from ServerName:
<VirtualHost *:80>
ServerName sis.name
ServerAlias sis.name
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName biblio.name
ServerAlias biblio.name biblio-intra.name
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
</VirtualHost>

Apache listen and redirect to a different port

I know this has been asked before, I used the highest rated question as a baseline, but I still can't get it to work.
I'm trying to host a website through an iocage jail in FreeBSD.
In my jail I'm hosting a domain called sub.domain.com. I've managed to get Apache to listen to a different port, 10080. So when I go to sub.domain.com:10080 I can access my website.
I would like to be able to go sub.domain.com and also access my website. As far as I understand, I need to redirect using mod_proxy to allow 80 -> 10080.
In httpd.conf I uncommented mod_proxy and added the following
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.sub.domain.com
ServerAlias sub.domain.com
ProxyPass / http://localhost:10080
ProxyPassReverse / http://localhost:10080
</VirtualHost>
This doesn't work for me, I still have to go to sub.domain.com:10080 to access my website.
Any tips?
EDIT: current config, also doesn't work
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName <jailip>
ServerAlias <jailip>
ProxyPass / http://<jailip>:10080
ProxyPassReverse / http://<jailip>:10080
</VirtualHost>

Redirect http to https by configuring .conf file of apache

I have configure apache to tomcat configuration by code like
<VirtualHost *:80>
ServerName captiveportal
ProxyPass / http://ip:port/path
ProxyPassReverse / http://ip:port/path
</VirtualHost>
Now i want to reirect this request to https
How can i achieve this ?
After looking your answer i have changes my configuration like
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "/etc/httpd/conf/crt1.crt"
SSLCertificateKeyFile "/etc/httpd/conf/key1.key"
ProxyPass / http://ip:port/path
</VirtualHost>
<VirtualHost *:80>
ServerName captiveportal
Redirect / https://ip:port/path
</VirtualHost>
but when i type captiveportal on my browser it redirects me on url https://ip:port/path and it displays problem loading page
One more thing i don't want to display https://ip:port/path on browser.
Note :- https://ip:port/path where port is my tomcat port and ip is machine ip where tomcat run.
You could do something like this:
<VirtualHost *:80>
ServerName captiveportal
Redirect / https://my.host.name/
</VirtualHost>
...and then put your ProxyPass directives in side your SSL VirtualHost block instead.

Apache host header proxy

I have multiple urls coming into a server. I want to user host headers to redirect the traffic. I am trying to use Apache to redirect these requests to various servers that are inside our firewall. I have gotten part of the solution, but, I seem to be missing something.
For example, http://hostHeader1.mycompany.com should be redirected to a server inside our firewall that handles requests for hostHeader1, and the result should be handed back to the client. http://hostHeader2.mycompany.com should be redirected to a server inside our firewall that handles requests for hostHeader2. Etc.
Right now, I have the following, but, it redirects all traffic to http://hostHeader1Handler/:
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://hostHeader1Handler/
ProxyPassReverse / http://hostHeader1Handler/
ServerName hostHeader1.mycompany.com
</VirtualHost>
Any help appreciated.
Scott
This is probably your first or your only virtual host. Just add another virtual host before. Then this should be the new default.
NameVirtualHost *:*
<VirtualHost *:*>
ServerName your.default.domain.de
DocumentRoot /var/www/pathToHTML
</VirtualHost>
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://hostHeader1Handler/
ProxyPassReverse / http://hostHeader1Handler/
ServerName hostHeader1.mycompany.com
</VirtualHost>