Shiny server ProxyPass using apache - 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.

Related

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.

Deploying a Grails app with Tomcat on an existing site

I am trying to deploy a grails app on an existing site (mysite.org) using Tomcat with a virtual host, and I've been told I also need to use a ProxyPass and a ProxyPathReverse to the chosen port. I've successfully set up Tomcat, added the WAR file to the tomcat/default-root folder, and edited the server.xml file to include this, an exclusion for serving content to the app.
<VirtualHost *:*>
ProxyPreserveHost On
ProxyTimeout 3600
Timeout 3600
ProxyPass /interventions !
ProxyPass / http://00.00.000.000:8080/
ProxyPassReverse / http://00.00.000.000:8080/
ServerName interventions.mysite.org
</VirtualHost>
I'm not really sure where to go from here, what I want to happen is to be able to go to interventions.mysite.org and use this app. I know I need to properly configure the virtual host but I've gotten lost in guides that seem to focus on setting it up from the start rather than integrating with an existing site, which had me worried about making any changes without realising (given my lack of knowledge right now).
What should my next step be, and are there any resources I should seek out (or search terms I should use, as I'm totally overwhelmed after my attempts)?
**Edit: Is my wishing to use interventions.mysite.org rather than, say, mysite.org/interventions complicating the issue?
create a site in apache called interventions.mysite.org.conf
To create the site you can just copy the already existing site file found in the sites-available folder in your apache installation and just make edits where necessary.
You file should look like the one i have pasted below, i believe the code i have pasted below should work fine for you, just make edits to the ProxyPass and ProxyPassReverse fields to match your app installation.
Remember to enable the site by using the command below;
sudo a2ensite interventions.mysite.org.conf
and also ensure that you have i think mod_proxy enabled.
it can config just like the one below;
<VirtualHost *:80>
ServerName interventions.mysite.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyRequests Off
ProxyPreserveHost On
ProxyErrorOverride On
ProxyPass / http://localhost:port/my-app/
ProxyPassReverse / http://localhost:port/my-app/
</VirtualHost>
You can also read the apache docs if you don't understand some of the config parameters. Hope that helps.
Cheers!

change ip address to domain name without redirect

I've been surfing for hours and I have not seen an answer that properly resolves my problem.
I have two pages in two different servers. They have to stay that way. One server has a domain name "domain.com" that show one web and it has a button with a link to the other server with the page ip:port/directory/page.php.
What i need is that, in the second server, even if it's coming from the first server or by direct access,the page always shows in the URL bar as domain.com/page.php, not ip:port/directory/page.php.
I've tried with virtualhost in the first server's httpd-vhost.confarchive, and changing the .htaccess file in ip:port/directory/ with RewriteRule, but nothing properly works.
Thanks for the advice and sorry for my English.
Finally solved using proxypass and proxyreverse in httpd.conf:
ProxyRequests off
ProxyPass /first_server_folder/ http://ip:port/second_server_folder/
ProxyPassReverse /first_server_folder/ http://ip:port/second_server_folder/
<Location /first_server_folder/>
ProxyPassReverse /
RequestHeader unset Accept-Encoding
</Location>

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>

AJP proxy that maps internal servlet name to a different external name

Using apache2 I want to set up an AJP proxy for a Tomcat server that maps an internal servlet URL to a completely different URL externally. Currently I am using the following configurations:
Apache2 configuration:
<IfModule mod_proxy.c>
ProxyPreserveHost on
ProxyPass /external_name ajp://192.168.1.30:8009/servlet_name
ProxyPassReverse /external_name ajp://192.168.1.30:8009/servlet_name
</IfModule>
Note that external_name and servlet_name are different.
Tomcat 6 configuration:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
This however does not work. Apache seems to forward http requests to Tomcat.
However the URLs and redirects returned by Tomcat are still using the original servlet_name and Apache does not map them to external_name.
Is this possible at all with AJP? If not can it be done using a plain http proxy instead?
Mapping different names between Apache and Tomcat can be quite tricky and depends much on how the web application builds its urls for the response.
Basically your setup is correct, but if your application uses its own servlet_name for redirects and urls ProxyPassReverse won't map them.
If you need this kind of setup have a look at mod_proxy_html (Apache 3rd party module) which will parse and rewrite also the contents, not only the url and response headers as mod_proxy.
( A late answer, but I just ran into this problem myself. )
It appears that ProxyPassReverse using ajp: doesn't work because the headers returned from a redirect don't have an ajp: URL in Location:, they have a http: URL. ProxyPassReverse just causes a rewrite of matching headers, and
that string doesn't match what's being returned.
This should work (provided the Location: field uses that numerical address
and not a host name.)
ProxyPreserveHost on
ProxyPass /external_name ajp://192.168.1.30:8009/servlet_name
ProxyPassReverse /external_name http://192.168.1.30/servlet_name
( You can use 'curl -I' to inspect the redirect headers and debug. )
See this note, or a more involved solution here using mod_proxy_html
for rewriting the URLs in web pages as well.
Additionally to the answer from Steven D. Majewski there is one more problem. If the target application uses the request host name to create a redirect (302 Moved Temporarily), it won't work with multiple host names. One must create multiple configurations for every name, like this:
ProxyPassReverse /external_name http://server.com/servlet_name
ProxyPassReverse /external_name http://server.org/servlet_name
ProxyPassReverse /external_name http://server.co.uk/servlet_name
Actually the ProxyPreserveHost on must solve this issue and replace the HOST header in the incoming requests with the address or IP specified in ProxyPass. Unfortunately it seems to be the ProxyPreserveHost doesn't work with ajp connectors. The tomcat in my configuration still received the host name got from browser instead replacing it with 192.168.1.30. As result the browser based redirects still didn't work for every name.
Following configuration didn't work as well :-(
# NOT WORKING !!!
ProxyPassReverse /external_name http://%{HTTP_HOST}/servlet_name
The workaround was using http instead of ajp.
ProxyPreserveHost on
ProxyPass /external_name ajp://192.168.1.30:8009/servlet_name
ProxyPassReverse /external_name http://192.168.1.30/servlet_name
Did somebody investigate it deeply?
For me, this seemed to cause problems:
ProxyPreserveHost on
ProxyPass /external_name ajp://192.168.1.30:8009/servlet_name
ProxyPassReverse /external_name http://192.168.1.30/servlet_name
While this seemed to work:
ProxyPreserveHost on
ProxyPass /external_name ajp://192.168.1.30:8009/servlet_name
ProxyPassReverse /external_name ajp://192.168.1.30:8009/servlet_name
I don't know why but it just did.