Howto serve a second subdomain through Apache Proxy - apache

I have an Apache frontend server, that until today proxies traffic for a subdomain api.myapp.net to a backend server running a Rails app on an Nginx.
Now I added a second subdomain alpha.myapp.net in my domain portfolio and gave it the same IP. Traffic to that subdomain shall not hit the Rails application, but a second VHost on the Nginx server that is setup to serve a static site.
So I have a proxy config for api.myapp.net:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#myapp.net
ServerName api.myapp.net
ServerAlias api.myapp.net
DirectoryIndex index.html
RewriteEngine On
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 9
RewriteCond %{HTTP_HOST} !^(api\.)?myapp\.net$
RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301]
ProxyPass / http://192.168.1.145/
ProxyPassReverse / http://192.168.1.145/
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature Off
</VirtualHost>
And I setup a second config for alpha.myapp.net:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#myapp.net
ServerName alpha.myapp.net
ServerAlias alpha.myapp.net
DirectoryIndex index.html
RewriteEngine On
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 9
RewriteCond %{HTTP_HOST} !^(alpha\.)?myapp\.net$
RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301]
ProxyPass / http://192.168.1.145/
ProxyPassReverse / http://192.168.1.145/
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature Off
</VirtualHost>
What happens now, is, that all traffic to alpha.myapp.net hits my Rails application that listens for requests for api.myapp.net.
I sorted out all Nginx config problems, so I thought it must be an Apache misconfiguration. What I see in Apache's rewrite log seems to explain the problem:
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (2) init rewrite engine with requested uri /index.html
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (3) applying pattern '^(.*)$' to uri '/index.html'
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (4) RewriteCond: input='alpha.myapp.net' pattern='!^(alpha\.)?myapp\.net$' => not-matched
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (1) pass through /index.html
The last part pass through /index.html seems to omit the subdomain and domain part. So the Nginx backend server does not now, which subdomain is requested, ans serves the request from the first available server, which ist api.
The question now seems to be: How can I proxy the traffic from Apache frontend to Nginx backend and maintain the subdomain and domain?
Or could there be another problem?
Hope someone can help.
Regards Felix

Sometimes the answer comes along with the question. The problem was exactly the missing hostname.
I solved the problem by editing /etc/hosts on my Apache server and adding two entries. One api.myapp.net, one alpha.myapp.net both referencing the same IP.
Then I changed bot Apache proxy configurations, so that ProxyPass and ProxyPassReverse do not use IP's anymore but the new hostnames.
And voila it works.

Related

Apache not loading document root correctly

Apache shows default page and doesn't load site configuration. OS: Debian 10.
Site is enabled but somehow it doesn't show files from public_html folder. Any help is appreciated.
<VirtualHost *:443>
SSLEngine On
ServerAdmin admin#abc.com
ServerName abc.com
ServerAlias *.abc.com
DocumentRoot /home/xx/public_html
SSLEngine on
SSLCertificateFile /home/xx/ssl.cert
SSLCertificateKeyFile /home/xx/ssl.key
<Directory /home/xx/public_html>
Require all granted
</Directory>
ErrorLog /home/xx/logs/error.log
CustomLog /home/xx/logs/access.log common
LogLevel debug
</VirtualHost>
No enough reputation to comment, so I’m trying with an answer and will clean it up if useful.
No mention of what you’re finding, if anything, in your logs. I assume you’re accessing using HTTPS to be sure your requests are going to port 443, but if per chance you were not I would try that first by specifying the protocol when entering the URL in your browser - otherwise you are probably making your request to the server on port 80 and not 443 where your VirtualHost is listening.
http://example.com ====> browser sends request to port 80, default port for http
https://example.com ====> browser sends request to port 443, default port for https
Is there also a VirtualHost entry for port 80 to redirect those requests to 443? If your browser is trying to load it as http using port 80 first then perhaps that’s why you’re seeing the Apache default page as I believe the server will be attempting to serve from /var/www/html/ for requests on port 80 unless you have already pointed these elsewhere with another VirtualHost, etc.
An example of what I mean that I have in use; either the ReWriteEngine or the Redirect permanent may be redundant, but I can confirm it functions fine for me as follows:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
For my setup, I preferred that the www subdomain be default and set up DNS intentionally to treat it as such, so the bare domain is ServerAlias in my instance.

Apache Virtual Hosts Non-www not working

I'm setting up a Virtual Hosts file on my CentOS 7 box and I'm having trouble getting my domain to resolve correctly.
Here's what my current /etc/httpd/conf.d/vhost.conf file looks like
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/domain.com/public_html/
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
It seems the the correct redirects are happening. For exmaple:
domain.com redirects to https: //www.domain.com
www works fine
BUT
https: //domain.com doesn't work
http ://domain.com doesn't work
In fact, if I remove the redirects I have set, domain.com ins't working at all, so it looks like the ServerAlias is broken?
I'm wondering if I need another redirect or is there some other step I'm missing?
Also, don't mind the spaces between http and the domain name. StackOverflow made me format it that way.
As presented, no request to anything https will ever work. Normal, you only have a VirtualHost on port 80. You do have a Listen directive for that port right?
For your redirections. It says: if you ask for http://www.example.com or http://example.com, redirect to https://<WHAT THE USER ASKED FOR>. In essence you are forcing your users to use https all the time, no problem there. But you do not have a VirtualHost on port 443, hence no response.
So:
Listen *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ErrorLog /var/log/httpd/80_error.log
CustomLog /var/log/httpd/80_access.log combined
RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
Listen *:443
<VirtualHost *:443>
ServerName www.example.com
# in case users do directly to https
ServerAlias example.com
DocumentRoot /var/www/html/domain.com/public_html/
DocumentIndex index.html
ErrorLog /var/log/httpd/443_error.log
CustomLog /var/log/httpd/443_access.log combined
# SSL CONFIGURATIONS, TODO!
</VirtualHost>
In your *:443 VH, you will have to configure certificates and SSL.
Your certificates will have to be valid for both www.example.com and example.com to avoid browser complaints.
Careful there might be an ssl.conf included file under conf.d that defines some of this. Make sure you only set it once to avoid confusion.
No need to define DocumentRoot in *:80 VH since it only redirects and does not respond content to client.
Have fun!
I solved the issue. I had my local hosts file configured to point to an old out of date IP address……
domain.com *bad ip address*
I'm so embarrassed. I must have set that up months ago and forgot.

Apache proxypass - show local file if exist

i use apache proxypass to show content from other server to my base server
i use this code
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/html/2
ServerName 2.example.com
ProxyPass /tv http://t1.example.com/tv/
ProxyPassReverse /tv http://t1.example.com/tv/
ErrorLog logs/errorlive_log
CustomLog logs/access_live common
</VirtualHost>
so is there a way first to check if file exist on 2.example.com (/var/www/html/2) if file exist show from this server and if file dont exist then request and server from t1.example.com/tv/
-and i have second question:
if server2 serve a video which is located in server1 and on server 2 are watching 10 users (10mbps) , so from which server will be taken 10mbps from server 2 or server 1 or both servers will have 10mbps load
In apache doc,you can see Ordering ProxyPass and RewriteRule Directives
RewriteRule directives are evaluated before ProxyPass ones.
So, you can add a rewrite rule which test if file exist
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/html/2
ServerName 2.example.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
ProxyPass /tv http://t1.example.com/tv/
ProxyPassReverse /tv http://t1.example.com/tv/
ErrorLog logs/errorlive_log
CustomLog logs/access_live common
</VirtualHost>
RewiteCond test if %{REQUEST_FILENAME} is a regular file and then rewriteRule rewrites to the file. It can be a html, image, php file, etc ...
Now, you can adapt to your need.
EDIT
For second question, I forgot to answer. Sorry, my bad.
According to Apache mod_proxy documentation :
"A reverse proxy (or gateway), by contrast, appears to the client just like an ordinary web server. No special configuration on the client is necessary. The client makes ordinary requests for content in the namespace of the reverse proxy. The reverse proxy then decides where to send those requests and returns the content as if it were itself the origin."
So, both server are loaded.

How to correctly set Documentroot in Apache serving Plone

I have a Plone site called example.com located at /var/www/Plone (I think). I have the following settings for the site located in sites-available for vhosts (excerpt):
<VirtualHost 10.0.1.4:8082>
ServerAdmin webmaster#localhost
ServerName wiedhas.noip.me
DocumentRoot /var/www/Plone
When I try to reach my site wiedhas.noip.me, apache loads the Plone directory tree and not my Plone site. I can browse through the file system of /var/www/Plone but it is not loading the site. I must not have set the documentroot to the correct directory of my site? Any help much appreciated.
This an excellent docu about running plone behind apache and more.
http://docs.plone.org/manage/deploying/front-end/apache.html
A simple example with ssl, how a vhost could look like:
<VirtualHost $IP:80>
ServerName my.domain.com
Redirect / https://my.domain.com
</VirtualHost>
<VirtualHost $IP:443>
ServerName my.domain.com
ErrorLog logs/my.domain.com-http-error.log
CustomLog logs/my.domain.com-http-access.log combined
Include vhosts.d/....ssl.inc
RewriteEngine On
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
</VirtualHost>
The most important part is the rewrite rule:
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
$PORT_OF_PLONE = Port of your running plone instance
/zodb/path/top/plone = That's where you added the plone site in zope.
Took me a while to get mine going so maybe this helps:
My vhosts looks like this (where my plone site is called 'mywebsite'):
#---------------------------------
# www.mywebsite.com
#---------------------------------
<VirtualHost *:80>
ServerName www.mywebsite.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/(.*)$ http://127.0.0.1:8081/VirtualHostBase/http/%{SERVER_NAME}:80/mywebsite/VirtualHostRoot/$1 [L,P]
</IfModule>
</VirtualHost>
Hope that helps :)
As you see in the first and correct answer you do not need DocumentRoot. DocumentRoot points to a directory with files to render by Apache. But Plone brings it's own server, the Zope application server, which runs on a different port than Apache. The RewriteRule redirects the incoming request to the application server and modifies the response in a way that the redirection is hidden for the client.

redirection issue in Apache

I have enabled https and can navigate application using HTTP and HTTPS without rewrite. Apache 2.2.24. But I see a strange behavior:
Receiving 400 bad request if passing http://hostname.com/XXX but works fine with url/xxx/ Don’t know how / can help to redirect to https.
Also, enabled the rewrite with following in httpd.conf but don’t see a difference.
Also, HTTP redirection is not working.
Rewrite Engine
RewriteEngine On
now the rewriting rules
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://hostname.com/XXX [R,L]`
The simpliest way to do that is to make 2 virtualhosts, one listening on IP:80 (HTTP) redirecting to HTTPS and the other listening on IP:443 (HTTPS).
<VirtualHost private_ip_of_your_server:80>
ServerName www.mywebsite.com
ErrorLog /var/log/apache/http_www_mywebsite_com_error.log
CustomLog /var/log/apache/http_www_mywebsite_com_access.log combined
RedirectPermanent / https://www.mywebsite.com/
</VirtualHost>
<VirtualHost private_ip_of_your_server:443>
ServerName www.mywebsite.com
ErrorLog /var/log/apache/https_www_mywebsite_com_error.log
CustomLog /var/log/apache/https_www_mywebsite_com_access.log combined
</VirtualHost>
Don't forget to add the two associated NameVirtualHost directives:
- NameVirtualHost private_ip_of_your_server:80
- NameVirtualHost private_ip_of_your_server:443
Regards