Apache domain redirect between virtualservers - apache

I've got one dedicated server which I splitted into more virtual servers.
On main server I use standard port for http (80), but for others I was forced to set different ports.
But I've got some spare domains.
What is best way to make invisible redirect to another server when user come through specific domain?
I don't want to use iframes or redirect to another website. I would like domain to act like on shared web hostings. But with different servers.
Is it possible to do?
I know that apache gets information about from which domain user came.
I would like to do it with virtual hosts if it's possible.
<VirtualHost *>
ServerName mydomain
ServerAlias mydomain
some redirection
CustomLog /var/log/apache2/mydomain.access.log combined
ServerAdmin myemail
</VirtualHost>
Thanks in advance :]

Since you have access to the server's config, take a look at the ProxyPass, ProxyPassMatch, and ProxyPassReverse directives that are part of mod_proxy. You'll need to make sure the module is loaded before you can use these directives.
In general, in your mydomain config, say you want to have visitors see the site at http://myother.domain.com/ when they go to http://mydomain/other, you'd just add:
ProxyPass /other http://myother.domain.com/
ProxyPassReverse /other http://myother.domain.com/
The ProxyPassReverse is to ensure proxied location responses get rewritten. For example, if a page at http://myother.domain.com/ returned a 301 redirect to http://myother.domain.com/newimage.gif, this directive will internal rewrite the response's location from http://myother.domain.com/newimage.gif to http://mydomain/other/newimage.gif, for it to be proxied again.
If you have cookie domains that also need rewriting, take a look at ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath.

Related

Apache virtualhost with a domain

I have an apache server with a bought domain.
I want to know if it is possible to redirect some web pages... For example
I have a NextCloud Server that I want to access by www.example.com/nextcloud
And a plex server I want to access by www.example.com/plex
PD: I don't have the possibilities of subdomains like www.plex.example.com because I didn't hire it when I bought the domain
Is this possible? How do I need to configure apache virtualhost? Thanks!
You mention that you want to access by www.example.com/nextcloud and by www.example.com/plex. I will therefore take for granted that you do not want the site address to change in your client's browser. So no redirection here. Redirection would change the address bar value.
Then the option you want is a reverse proxy. It will "hide" the fact that the client is being served pages by another site or application.
Assumptions:
You have system 1 with an Apache server that responds to http://www.example.com
You have system 2 with an application that responds to http://www.domain1.com/nextcloud.
You have system 3 with an a plex application that reponds to http://www.domain2.com/plex
Therefore on system 1, in the configuration file for your Apache (most probably httpd.conf), you will:
load the proxy modules
add these lines in your <VirtualHost>:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
[... SOME OTHER CONFIG ...]
ProxyPass "/nextcloud" "http://www.domain1.com/nextcloud"
ProxyPassReverse "/nextcloud" "http://www.domain1.com/nextcloud"
ProxyPass "/plex "http://www.domain2.com/plex"
ProxyPassReverse "/plex" "http://www.domain2.com/plex"
[... SOME OTHER CONFIG ...]
</VirtualHost>
Now domain1.com and domain2.com can be IP addresses, but using dns is so much better for flexibility. Adjust this sample as required.
Complete mod_proxy documentation: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

How to set up redirect on Apache forward proxy?

I have Apache 2.4, and I want use it as local proxy server. This is my config of VirtualHost:
<VirtualHost 127.0.0.4:1596>
RewriteEngine on
ProxyRequests On
DocumentRoot "D:/USR/www/proxy"
ErrorLog "D:/USR/log/proxy/error.log"
CustomLog "D:/USR/log/proxy/access.log" common
ServerAdmin webmaster#bot-host
</VirtualHost>
I want to redirect traffic. For example, when user go to https://example1.com/somepage it will redirected to https://example25.com/
I also want to rewrite URL. For example, when user sent request https://example1.com/somepage?parm1=1596&param2=asdfg789 proxy server will send https://example1.com/somepage?parm1=1789&param2=kloxcfjiobvjx9
I know that this can be done using a mod_rewrite function, but I cannot do that. All examples I have seen are about how to redirect in my server pages, but I need a redirecting and rewriting a request on proxy server from other domains.
I don't want to use Squid, I need to do this with Apache.
So, I want to have a local proxy sever with some rules of redirecting and rewriting. Rewriting is very much needed.
So, when user is connected to proxy 127.0.0.4:1596 and go to https://example1.com/somepage, but in fact that server get content from example25.com , and in another case (it is most wanted by me) when user send https://example1.com/somepage?parm1=1596&param2=asdfg789 server is really send a https://example1.com/somepage?parm1=1789&param2=kloxcfjiobvj‌​x9 an get answer to user.

Allow internet users to access privately hosted website pages

I have a corporate private network(VPN) and on one VM a website is hosted which can be accessed internally only. e.g. https://internal.com/welcome.html
Now, I want to allow few pages of the site to be accessible from outside but with their own url.
e.g. they will open http://theirdoamin.com/welcome.html which will be redirected into my private network and internally it will be mapped/proxied to https://internal.com/welcome.html.
This way outside will never know the actual url (i.e https://internal.com/welcome.html.
My question is, can we achieve this using Apache Reverse proxy server sitting in-front of my hosted VM?
Second question, can I also limit the access to welcome.html page only and not others?
My colleague already implemented using Apache Nifi but I still believe it can be simple done using Apache Reverse Proxy setup.
Please advise.
Thanks
1) Yes, Apache reverse-proxy is able to do that.
2) You can limit access as you like.
1) I'd set up two vhosts (for examples), one with the original name and one with the VPN-accessable name.
Listen 80
#If you are running a Apache 2.2 you'll need the following line, for 2.4 you won't
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "srv/www/example1"
ServerName internal.com
<Directory "/srv/www/example1">
Require all granted
</Directory>
# Other directives here, if needed
</VirtualHost>
<VirtualHost *:80>
ServerName external.com
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^ https://internal.com/welcome.html [P]
ProxyPassReverse / https://internal.com/
</VirtualHost>
You'll need the RewriteRule to target a specific file instead of a whole domain. Otherwise a simple ProxyPass would have been enough.
This requires the modules mod_rewrite, mod_proxy and mod_proxy_http to be activated.
2) is done by the RewriteRule. It only allows access to that specific target-file (welcome.html).

Apache 2.4 multiple applications on separate IPs under single domain

I am a little lost on how to achieve the following...
I have a single domain name which is running a CMS # www.mywebsite.com
If a specific URL is given apache calls the other VM (different IPv4) running a shop. www.mywebsite.com/store
I've trawled through apache vhosts but nothing seemingly covers the above scenario if its even possible ...https://httpd.apache.org/docs/2.0/vhosts/examples.html
Any guidance would be very much appreciated.
If I understand your problem correctly, you could do this with mod_proxy.
For example:
<VirtualHost *:80>
ServerName www.mywebsite.com
ProxyPass /store/ http://store.local/
ProxyPassReverse /store/ http://store.local/
</VirtualHost>
You could use the IP address, server hostname or whatever in the proxy directives - store.local is just an example.
The mod_proxy documentation is extensive.

Setting a new IP as a subfolder of an existing IP

I'm pretty new to Apache configurations... is the following possible?
I have 2 separate web servers, each hosting a different application - totally separate.
My main application is under the domain www.example.com. What I want to do is set the other server's domain to something like www.example.com/newapp so that when users go to this URL, they will be redirected to the 2nd server (which is a totally different domain/IP/virtualhost); pages under this second server's url will always be www.example.com/newapp/xxxx.
NOTE: there is nothing matching this directory structure under the first, main application.
Basically, the www.example.com/newapp/ application is completely separate, but I want users to think its actually the same website.
You need to configure the first server handling all example.com requests to proxy requests to the new server when a request for example.com/newapp is received. This will cost you bandwidth on both sides, beware of that.
Using ProxyPass & ProxyPassReverse should suffice. If you want hyperlink conversion as well, you need to use mod_proxy_html
ProxyPass /newapp/ http://xx.xx.xx.xx/newapp
ProxyPassReverse /newapp/ http://xx.xx.xx.xx/newapp
mod_proxy_html: http://apache.webthing.com/mod_proxy_html/
<VirtualHost *:80>
UseCanonicalName Off
ProxyPass /newapp/ http://xx.xx.xx.xx/
ProxyPassReverse /newapp/ http://xx.xx.xx.xx/
Include /etc/apache2/conf/railsapp.conf
</VirtualHost>