Multiple domains, multiple vhosts, separate rails apps - ruby-on-rails

I am trying to host multiple different rails apps on the same VPS using multiple domains. I am using Apache 2.2.17 on Ubuntu 10.10. For Apache, I have multiple vhost files so that I can enable and disable particular sites easily without needing to comment them out or delete them. In addition, I am also using mod_rewrite so that the multiple domains that go to the same rails app appear to go to the same URL so I don't take a hit with search engines as far as duplicate content.
I believe that my DNS is setup properly. For each of the domains, I have a www subdomain as well as some site-specific sub-domains, such as blogs, etc. The issue I am seeing is that Apache appears to matching the www subdomain immediately and not examining the additional URL behind it. Changing ServerAlias does nothing. For example, using my setup, if I enter davidheartsrachel.com, I properly reach my wedding website. However, if I use www.davidheartsrachel.com, I reach my other website, my software development business. The URL is not re-written; it stays as davidheartsrachel.com instead of afewguyscoding.com.
The only way I have been able to properly get it to work is to use mod_rewrite in the primary vhost file to redirect to the wedding website vhost file (you can see that I have that in the primary vhost file, but it is commented out for purposes of this question). This doesn't seem proper to me? Should I get another IP and do IP vhosts instead of name-based vhosts?
When I execute apachectl -S, I get the following:
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 is a NameVirtualHost
default server afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
port 80 namevhost afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
port 80 namevhost blog.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:69)
port 80 namevhost lbbs.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:84)
port 80 namevhost davidheartsrachel.com (/etc/apache2/sites-enabled/davidheartsrachel.com:1)
port 80 namevhost dhr.afewguyscoding.com (/etc/apache2/sites-enabled/davidheartsrachel_staging:1)
Syntax OK
I notice that afewguyscoding.com is the default site - however, doesn't it have to do a full string match to determine the proper site?
Primary site's vhost file
<VirtualHost *:80>
ServerAdmin david.stites#afewguyscoding.com
ServerName afewguyscoding.com
ServerAlias davidstites.com, 5280software.com, milehigh-software.com, milehighsoftware.org
ServerAlias www.5280software.com, www.milehigh-software.com, www.milehighsoftware.org, www.davidstites.com, www.afewguyscoding.com
# this tells rails that it will run in production mode
# this is for rails < 3.x
RailsEnv production
DocumentRoot /var/www/afewguyscoding/current/public
DirectoryIndex index.html
# custom log file locations
# possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel alert
ErrorLog /var/www/afewguyscoding/current/log/error.log
CustomLog /var/www/afewguyscoding/current/log/access.log combined
# allows compression of text based mime.types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
FileETag None
RewriteEngine On
# check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{REQUEST_URI} !^/ws/
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html
#RewriteCond %{HTTP_HOST} ^www.davidheartsrachel.com$
#RewriteRule ^(.*)$ http://davidheartsrachel.com$1 [L]
RewriteCond %{HTTP_HOST} ^www.davidstites.com$
RewriteRule ^(.*)$ http://www.afewguyscoding.com$1 [R=301,L]
<Directory /var/www/afewguyscoding/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# In case I ever need CGI
#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
#<Directory "/usr/lib/cgi-bin">
# AllowOverride None
# Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
# Order allow,deny
# Allow from all
#</Directory>
# how we can restrict access to documents from the local subnet
#Order deny,allow
#Deny from all
#Allow from 127.0.0.0/255.0.0.0 ::1/128
<Location /blog>
PassengerEnabled off
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin info#afewguyscoding.com
ServerName blog.afewguyscoding.com
DocumentRoot /var/www/wpress
DirectoryIndex index.php
<Directory /var/www/afewguyscoding/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Wedding site's vhost file
<VirtualHost *:80>
ServerAdmin info#davidheartsrachel.com
ServerName davidheartsrachel.com
ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com
ServerAlias www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
# this tells rails that it will run in production mode
# this is for rails < 3.x
RailsEnv production
# this is for rails >= 3.x
RackEnv production
DocumentRoot /var/www/davidheartsrachel/current/public
DirectoryIndex index.html
# Custom log file locations
# Possible values include: debug, info, notice, warn, error, crit, alert and emerg,
LogLevel alert
ErrorLog /var/www/davidheartsrachel/current/log/error.log
CustomLog /var/www/davidheartsrachel/current/log/access.log combined
# Allows compression of text based mime types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
FileETag None
RewriteEngine On
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
RewriteCond %{HTTP_HOST} ^www.rachelanddavidwedding.com$
RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.rachelanddavidstites.com$
RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
# Static cache
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteCond /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html -f
RewriteRule ^(.*)$ /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html [L]
<Directory /var/www/davidheartsrachel/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
If you can think of any more information that might be helpful, please, ask me to post it.
Edit:
Let me try to clear this up. In the most essential form, my question is: why does going to davidheartsrachel.com takes you to www.afewguyscoding.com and davidheartsrachel.com takes you to davidheartsrachel.com based on my configuration above

On my servers I don't define a primary site in my main httpd.conf file. I do all the virtual hosting in separate application specific conf files.
The only thing I have in my main httpd.conf is the line NameVirtualHost *:80
Here's a gist of a way you could try it. I pulled all the wedding site configuration out of your main site configuration. I set it so davidstites.com, afewguyscoding.com, and www.davidstites.com all redirect to www.afewguyscoding.com. The other domains (milehigh et al) are unaffected. I'm not sure where you wanted those to go.
All the wedding related domains redirect to www.davidheartsrachel.com.
I did notice what I think might have been a mess up in your main site configuration. Toward the end where you set your directory permissions you were using the /var/www/afewguyscoding/current/public directory instead of the /var/www/wpress that is the DocumentRoot. The correction I made there is on line 74 of the first document in that gist.

The answer is that I was incorrectly separating the entries under ServerAlias with a comma thusly:
ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com, www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
It is supposed to be a space:
ServerAlias rachelanddavid.net rachelanddavidstites.com rachelanddavidwedding.com www.davidheartsrachel.com www.rachelanddavidstites.com www.rachelanddavidwedding.com www.rachelanddavid.net

Related

Apache2 reverse proxy to multiple docker-hosted sites (eg openhab)

I have found a lot of information and help in this forum, but I could not find any solution for my proxy-problem.
I've been searching various forums and trying different options for the past few weeks, but my reverse proxy just isn't working.
I have apache2 running on Ubuntu 20.04 with several websites and also a few Docker containers in use.
Now I want to make a redirect via reverse proxy to 2 Docker websites to save me the SSL certificates.
Locally the Docker containers work
http://server1:1234 => Openhab Docker
http://server1:89 => Tasmota WebAdmin
The Apache-Conf currently looks like this
server-ssl.conf
# NameVirtualHost *:443
SSLStrictSNIVHostCheck Off
<VirtualHost *:443>
ServerName server1.com
ServerAlias *.server1.com
ServerAdmin admin#server1.com
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
</IfModule>
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
RewriteEngine On
SSLCertificateFile /etc/ssl/private/server1.crt
SSLCertificateKeyFile /etc/ssl/private/server1.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory /var/www/html/>
DirectoryIndex index.htm index.html index.php
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
############################
# Beginn Reverse Proxy Settings
###########################
SSLProxyEngine On
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /openhab/ http://127.0.0.1:1234/
Header set Set-Cookie "X-OPENHAB-AUTH-HEADER=1"
ProxyPassReverse /openhab/ http://127.0.0.1:1234/
ProxyPass /tasmota/ http://127.0.0.1:89/
ProxyPassReverse /tasmota/ http://127.0.0.1:89/
####################
# Ende Reverse Proxy Settings
###################
The result of this config is
https://my.server1.com/openhab
I see the Title "Openhab" at the tab but no website, no openhab-Icons,... like at my Home-Net
Perhaps because Openhab is using Java?
https://my.server1.com/tasmota
shows "URL not found"
If I change it to
https://my.server1.com/tasmota/login
I see the login-page without formatting.
After Login the URL changes to
https://my.server1.com/devices
but should be
https://my.server1.com/tasmota/devices
If I change it manually, I see my devices but without formatting.
I have no idea what is wrong, I thought it ist adding ProxyPass and ReverseProxyPass and the whole thing runs.
Maybe someone has a tip for me?
Of course the modules proxy, proxy_http are running.
Thanks a lot and best regards

Proxy Pass Subdirectory in Apache Passenger with Rails App

I have a rails app (www.myapp.com) for which I am using Apache Passenger. The virtual host is configured as follows:
<VirtualHost *:80>
DocumentRoot "/var/www/myapp/current/public"
RailsEnv production
<directory "/var/www/myapp/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
RailsBaseURI /
</VirtualHost>
I have a blog which is hosted externally on some other domain, lets say www.myapp-blog.com. I want to 301 redirect any requests to www.myapp.com/blog or www.myapp.com/blog/* to the relevant pages on www.myapp-blog.com. Everything else should flow through my Rails app.
How should I modify the above virtual host configuration to achieve this?
use apache port forwarding technique...
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.myapp-blog.com
ServerAlias myapp-blog.com
ProxyPass / http://www.myapp.com/blog/
ProxyPassReverse / http://www.myapp.com/blog
</VirtualHost>

Apache Passenger configuration issue

I've been trying for days to get a rails app to run under Passenger on Bluehost, but with no luck. Going to my subdomain where I'm hoping this will run, I'm only getting a directory listing with cgi-bin and public subdirectories. No execution of the rails app.
httpd.conf includes:
LoadModule passenger_module /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.6/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.6
PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby
</IfModule>
and
<VirtualHost 162.144.138.43:80>
ServerName secure.xxxxx.com
ServerAlias www.secure.xxxxx.com
DocumentRoot /home/diamoou1/public_html/securefinance
ServerAdmin webmaster#secure.xxxxx.com
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/secure.xxxxx.com combined
CustomLog /usr/local/apache/domlogs/secure.xxxxx.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
## User diamoou1 # Needed for Cpanel::ApacheConf
UserDir enabled diamoou1
<IfModule mod_suphp.c>
suPHP_UserGroup diamoou1 diamoou1
</IfModule>
<IfModule !mod_disable_suexec.c>
<IfModule !mod_ruid2.c>
SuexecUserGroup diamoou1 diamoou1
</IfModule>
</IfModule>
<IfModule mod_ruid2.c>
RUidGid diamoou1 diamoou1
</IfModule>
ScriptAlias /cgi-bin/ /home/diamoou1/public_html/securefinance/cgi-bin/
<Directory /home/diamoou1/public_html/securefinance>
Options -MultiViews
AllowOverride All
</Directory>
</VirtualHost>
In /home/diamoou1/public_html/securefinance/public I have the .htaccess file:
<IfModule mod_passenger.c>
Allow from all
Order Deny,Allow
Options +FollowSymLinks -SymLinksIfOwnerMatch -MultiViews
PassengerResolveSymlinksInDocumentRoot on
#Set this to whatever environment you'll be running in
RailsEnv production
RackBaseURI /
SetEnv GEM_HOME /home/diamoou1/ruby/gems
</IfModule>
Temporarily putting a bad line in the .htaccess file will give the expected error in the log, so apparently apache is reading the .htaccess file.
I suspect the problem might be in RackBaseURI. I installed Passenger as root. RackBaseURI resolves to /root and there is a /root/public_html directory, although apache is serving up the /home/diamoou1/public_html/securefinance directory (where the rails app actually resides). I don't know how to change that variable or it that's really the problem. PassengerBaseURI is the same.
What do I do to fix this?????
This may help you : https://www.phusionpassenger.com/documentation/Users%20guide%20Apache.html#_tutorial_example_writing_and_deploying_a_hello_world_rack_application
Here is the example configuration provided by Phusion Passenger :
<VirtualHost *:80>
ServerName www.rackexample.com
DocumentRoot /webapps/rack_example/public
<Directory /webapps/rack_example/public>
Allow from all
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
#Require all granted
</Directory>
</VirtualHost>
DocumentRoot and Directory should point to the public folder of your rails application not the root.
You should change :
...
DocumentRoot /home/diamoou1/public_html/securefinance
...
<Directory /home/diamoou1/public_html/securefinance>
...
to :
...
DocumentRoot /home/diamoou1/public_html/securefinance/public
...
<Directory /home/diamoou1/public_html/securefinance/public>
...

Directory index forbidden by Options directive in ruby on rails

I Configured SSL for rails app running with thin server in CentOS linux environment with apache. When I try to open my site using "https" I am getting Apache error page and I checked apache error logs and got the following error message
Directory index forbidden by Options directive.
I made some changes in /etc/httpd/conf.d/welcome.conf as
"Options -Indexes" TO "Options +Indexes"
then I am getting directory structure in browser.
Please help me to solve the issue
Update:
<VirtualHost *:80>
ServerName XXXX
ServerAlias www.XXXX.com
DocumentRoot XXXX
RewriteEngine On
<Proxy balancer://thinservers>
BalancerMember http://127.0.0.1:3000
</Proxy>
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]
ProxyPass / balancer://thinservers/
ProxyPassReverse / balancer://thinservers/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog XXXX
CustomLog XXXX
Your included Apache configuration shows the HTTP virtual host as it's litening on port 80
<VirtualHost *:80>
HTTPS connects to a different virtual host defined on port 443 you will need to modify this virtualhost's configuration in order to change the way the HTTPS portion of your website behaves.

Deploy 2 Sites on same Apache server

I need to deploy my Rails app to a server that is already serving a PHP site at its root. I can't touch the existing site, and I have to deploy my app at a sub-url or sub-domain, ie xx.xx.xx.xx/rails or rails.xx.xx.xx.xx.
The Apache config I normally use to deploy my app:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/RubyStack-3.2.5-0/projects/app_name/public"
<Directory "C:/RubyStack-3.2.5-0/projects/app_name/public">
Allow from all
Options -MultiViews
</Directory>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://app_balancers%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://app_balancers>
BalancerMember http://localhost:3001/
BalancerMember http://localhost:3002/
</Proxy>
# Support for far-futures expires header
<LocationMatch "^/assets/.*$">
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</LocationMatch>
</VirtualHost>
How could I change this configuration to serve that folder from a sub-url or subdomain? A RedirectMatch won't work because it would also route people away from the existing PHP site, right?
Easiest way would be to do name-based virtual hosting, using a subdomain as you suggested. So where you write:
<VirtualHost *:80>
ServerName localhost
</VirtualHost>
Simply enter your subdomain instead:
<VirtualHost *:80>
ServerName mysubdomain.mycompany.com
</VirtualHost>
Apache should then separate any requests to that subdomain from the 'main' virtualhost automatically.
edit:
Alternatively it is possible to mount your rails app to a subdirectory as well (assuming you're using Passenger.) Here's an example based on my own local staging environment. I got this working by following the instructions at the Phusion website
<VirtualHost *:80>
ServerName localhost
# We will mount our application under http://localhost/myapp
RailsBaseURI /myapp
# This can be anywhere on the system, I just happened to use /home/www
<Directory /home/www/myapp>
# Here you can add any directives necessary for your app, like for example..
SetEnv GEM_HOME /home/user/.rvm/gems/ree-1.8.7-2012.02
</Directory>
</VirtualHost>
Now you have to do one more thing, and that's make /home/www/myapp a link to the public dir of the actual application. So let's say you have the application in your own homedir you would have to type this:
cd /home/www
ln -s /home/myuser/myapp/public myapp
If you then type ls -l it should show:
lrwxrwxrwx 1 myuser myuser 18 Jun 10 16:41 devb -> /home/myuser/myapp/public
I think that should be it.

Resources