Subdirectory on a different host - apache

I've got a site www.example.com on one server. I'd like to create www.example.com/blog on another server/host.
A) Can I do this?
B) How can I do this?
I've read a bit on using Apache's mod_proxy, proxypass, and ProxyPassReverse, but I'm not gifted enough with Apache or server mgt to know if I'm on the right track or not. Or, if there are other options to doing what I want.
To be clear, I do not want a subdomain like blog.example.com. I know how I COULD do that with DNS, but I don't believe DNS is an option for subdirectories.

This is not doable nicely IMO. As you say, you would need a proxy based solution that fetches the content from the blog server and serves it to the user.
That has the serious downside that any traffic on your blog will have to run through your primary host, eating up bandwidth, resources and traffic volume.
Is a redirect (i.e. www.domain.com/blog doing a header redirect [visible in the browser's address bar] to blog.otherhost.com) an option?
If not, check out the documentation on mod_proxy. The basic example for a reverse proxy looks good already:
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

Related

Shiny server ProxyPass using apache

My shiny server runs on my VPS without any issues (on Ubuntu, via Apache). So that's great. :) However, what I now try to achieve is the following (sorry for not using the correct terminology here, this is new for me):
Instead of sharing my apps via https://www.example.com:3838/appname, I would like to use https://www.example.com/appname (is this called port forwarding? aka 'getting rid of port 3838').
It is important to note that I do not want the shiny server to 'take over' my homepage as https://www.example.com serves my own personal site.
I tried the codeblock shared by Chris Beeley to be included in /etc/apache2/sites-enabled/000-default-le-ssl.conf, but that does not work. However, what does work to some degree is if I adjust the code as follows
Original code
ProxyPreserveHost On
ProxyPass /shinyapps http://0.0.0.0:3838/shinyapps
ProxyPassReverse /shinyapps http://0.0.0.0:3838/shinyapps
ServerName localhost
Adjusted code
ProxyPreserveHost On
ProxyPass /foo http://0.0.0.0:3838/foo
ProxyPassReverse /foo http://0.0.0.0:3838/foo
ServerName localhost
Yes, my app called foo can now be reached by www.example.com/foo; but this implies that I need to add the names of all my apps in this codeblock.
Any suggestions? Thank you.
Also, I am sorry if this question turns out to be duplicate.
PS. All incoming connections to http are already redirected to https
That should work:
ProxyPreserveHost On
ProxyPass /(.*)$ http://0.0.0.0:3838/$1
ProxyPassReverse /(.*)$ http://0.0.0.0:3838/$1
ServerName localhost
Check out this link: https://httpd.apache.org/docs/trunk/rewrite/intro.html
I think you've misunderstood the way the URLs are structured. You need to point to
ProxyPass /foo http://0.0.0.0:3838/foo
As you have done, but foo is the name of the directory of apps.
So take all the application folders that you have, and put them inside foo.
Now the URL will be
https://example.org/foo/app1
https://example.org/foo/app2
Etc.

How can I use apache2 on my Raspberry Pi to forward incoming subdomain requests to go to localhost ports running other applications?

I'm new to networking and web development and am trying to create a home project for fun, to learn, and to have some capabilities I would like from a home server. So far, I already own a domain name and have DDNS set up with Namecheap and port forwarding on my router so that incoming requests on port 80 get forwarded to my Pi on that port. I can access a basic index.html page using mydomain.com and www.mydomain.com.
I also want to be able to access some other applications running on my Pi remotely in the future. Two I'm working on currently are Webmin and Shellinabox. I can currently do this by going to mydomain.com:12321 and mydomain.com:4200 respectively. However, I would like to have this set up in a cleaner way so I can instead go to webmin.mydomain.com and ssh.mydomain.com instead. I am able to do that using Virtual Hosts and a redirect currently, but that changes the URL in the browser.
As a more advanced solution, I'm trying to use mod_proxy so the URL in my browser still shows the URL I typed. I've done some searching and think the best way to do this is using mod_proxy with virtual hosts, but I have not been able to get it to work.
For starters, I don't think I understand how that works fully. My basic understanding is that apache forwards an incoming request to another server and relays the reply back to the original requesting machine. However, in testing, I tried to do this:
<VirtualHost *:80>
ServerName ssh.mydomain.com
ProxyPreserveHost On
ProxyRequests Off
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://www.google.com
ProxyPassReverse / http://www.google.com
</VirtualHost>
My understanding would be that the Google homepage would show up when I navigated to ssh.mydomain.com, however I instead get a Google error page that says:
That’s an error.
The requested URL / was not found on this server. That’s all we know.
Clearly I'm misunderstanding this. Prior to this testing, I had that block of code configured like this:
<VirtualHost *:80>
ServerName ssh.mydomain.com
ProxyPreserveHost On
ProxyRequests Off
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:4200
ProxyPassReverse / http://localhost:4200
</VirtualHost>
I've also tried 0.0.0.0 and 127.0.0.1 instead of localhost, so I don't believe that's the issue. When I have it set up as shown above and navigate to that URL, I get this in Safari:
Safari Can't Connect to the Server
Safari can't open the page "https://ssh.mydomain.com" because Safari can't connect to the server "ssh.mydomain.com".
I can't help but wonder if the https is causing some issue. I'm not typing it into my browser, but it is shown in the page it's trying to connect to. I'm just not sure at this point. I could make it functional, sure, but I want to learn how to do it the way I'm trying here. I'm open to other ideas that would keep the browser URL instead of redirects if there are other ways that don't use mod_proxy, but from other places I've read, this seems to be the way. Thanks in advance for your help.

Apache domain redirect between virtualservers

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.

How to rewrite / proxy an Apache URI to an application listening on a specific port / server?

They say that Apache's mod_rewrite is the swiss-army knife of URL manipulation, but can it do this?
Lets say I want to add a new application to my Apache webserver, where the only configurable option of the app is a port number.
I want to use & give out URLs of the form "http://hostname.example.com/app" rather than "http://hostname.example.com:8080". This would ensure that clients would be getting through the institution's firewall as well, and it's generally tidier.
My application includes absolute URIs in php, javascript and css, so I want to prepend my own root location to the URI in the applications internal links. I have no access to DNS records and so can't create another name-based virtual server.
Using Apache's mod_rewrite and mod_proxy modules, I can transparently redirect a client to the correct home-page of the application. But links within that homepage don't point a client to links relative to the new base URL.
So, what's the best way of proxying a request to an application that is listening on a specific port?
For example, if I had an application listening on port 8080, I could put this in my Apache configuration:-
<VirtualHost *:80>
SSLProxyEngine On
ServerName myhost.example.com
RewriteEngine On
UseCanonicalName On
ProxyVia On
<Location "/application">
RewriteRule ^/application/?(.*) http://localhost:8080/$1 [P,L]
</Location>
</VirtualHost>
This would work fine if the application didn't use absolute URLs, but it does. What I need to do is rewrite URLs that are returned by the application's css, javascript and php.
I've looked at the ProxyPass and ReverseProxyPass documentation, but I don't think these would work..?
I've also come across Nick Kew's mod_proxy_html, but this isn't included in the standard Apache Distribution, and my institution's webserver seems to have been fine for years without it.. Other than trawling manually (or using a grep -r | sed type expression) through the application's source code, or using this 3rd party add-on, are there any other ways to go about this?
Could I perhaps use some of the internal server variables in a mod_rewrite rule? For example a rewrite rule based on ’HTTP_REFERER'?
Using mod_proxy would work just fine. For instance, I mapped https://localhost/yalla/ to point to a subdirectory of my webserver:
LoadModule proxy_module modules/mod_proxy.so
ProxyRequests On
<Proxy *>
Order deny,allow
Allow from localhost
</Proxy>
ProxyPass /yalla/ http://yalla.ynfonatic.de/tmp/
If you implement this, you'll note that the pictues of the directory-listing aren't visible; this is because they're below the /tmp/ directory on the remote server, hence not visible.
So, in your case you'd do:
LoadModule proxy_module modules/mod_proxy.so
ProxyRequests On
<Proxy *>
Order deny,allow
Allow from localhost # Or whatever your network is if you need an ACL
</Proxy>
ProxyPass /app/ http://hostname.example.com:8080/
Like with everything in Apache configuration, watch those trailing slashes when referring to directories.
Good luck!
Alex.

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>