Setting up virtual hosts for Rails apps on Apache and Mongrel
I have a single Linux server running 3 site on Apache. Let's call them RailsApp1, RailsApp2, and SimpleApp. Both Rails applications are using Mongrel clusters. The other application is just a single HTML file. I have different Virtual Host files setup in Apache for each site, as well as mongrel_cluster.yml files for both Rails sites (code for all this is at the bottom).
With everything setup, I can enable the sites in Apache just fine. And I can start the Mongrel clusters for each Rails site just fine. And, in fact, visiting www.simpleapp.com and www.railsapp1.com in my browser works just fine. However, www.railsapp2.com gives me a ton of trouble. Instead of showing the code for railsapp2, the server returns the HTML for railsapp1. If I disable railsapp1 in Apache and then go to www.railsapp2.com, the server now returns the HTML for simpleapp. Only if I disable both railsapp1 and railsapp2 in Apache will the server correctly respond to a request at www.railsapp2.com.
Any thoughts on why this might be happening?
SimpleApp's VHOST File:
<VirtualHost *:80>
ServerName www.simpleapp.com
ServerAlias simpleapp.com
DocumentRoot /home/nudecanaltroll/public_html/simpleapp
</VirtualHost>
RailsApp1's VHOST File:
<VirtualHost *:80>
ServerName railsapp1.com
DocumentRoot /home/nudecanaltroll/public_html/railsapp1/public
RewriteEngine On
<Proxy balancer://mongrel1>
BalancerMember http://127.0.0.1:5000
BalancerMember http://127.0.0.1:5001
BalancerMember http://127.0.0.1:5002
</Proxy>
# Timeout in 30 seconds
ProxyTimeout 30
# Make sure people go to www.railsapp1.com, not railsapp1.com
RewriteCond %{HTTP_HOST} ^railsapp1\.com$ [NC]
RewriteRule ^(.*)$ http://www.railsapp1.com$1 [R=301,NE,L]
# Redirect all non-static requests to thin
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/mongrel1(.*)开发者_运维问答$ balancer://mongrel1%{REQUEST_URI} [P,QSA,L]
# Proxy Stuff
ProxyPass / balancer://mongrel1/
ProxyPassReverse / balancer://mongrel1/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog /home/nudecanaltroll/public_html/railsapp1/log/error.log
CustomLog /home/nudecanaltroll/public_html/railsapp1/log/access.log combined
</VirtualHost>
RailsApp2's VHOST File:
<VirtualHost *:80>
ServerName railsapp2.com
DocumentRoot /home/nudecanaltroll/public_html/railsapp2/public
RewriteEngine On
<Proxy balancer://mongrel2>
BalancerMember http://127.0.0.1:6000
BalancerMember http://127.0.0.1:6001
BalancerMember http://127.0.0.1:6002
</Proxy>
# Timeout in 30 seconds
ProxyTimeout 30
# Make sure people go to www.railsapp2.com, not railsapp2.com
RewriteCond %{HTTP_HOST} ^railsapp2\.com$ [NC]
RewriteRule ^(.*)$ http://www.railsapp2.com$1 [R=301,NE,L]
# Redirect all non-static requests to thin
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/mongrel2(.*)$ balancer://mongrel2%{REQUEST_URI} [P,QSA,L]
# Proxy Stuff
ProxyPass / balancer://mongrel2/
ProxyPassReverse / balancer://mongrel2/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog /home/nudecanaltroll/public_html/railsapp2/log/error.log
CustomLog /home/nudecanaltroll/public_html/railsapp2/log/access.log combined
</VirtualHost>
RailsApp1's mongrel_cluster.yml File:
---
address: 127.0.0.1
log_file: log/mongrel.log
port: 5000
cwd: /home/nudecanaltroll/public_html/railsapp1
environment: production
pid_file: /home/nudecanaltroll/public_html/railsapp1/tmp/pids/mongrel.pid
servers: 3
RailsApp2's mongrel_cluster.yml File:
---
address: 127.0.0.1
log_file: log/mongrel.log
port: 6000
cwd: /home/nudecanaltroll/public_html/railsapp2
environment: production
pid_file: /home/nudecanaltroll/public_html/railsapp2/tmp/pids/mongrel.pid
servers: 3
I figured it out. For reasons unknown to me, I needed to set the ServerAlias for RailsApp2, and also add "www." in front of ServerName. So the top of the railsapp2.com vhost file now looks like this:
<VirtualHost *:80>
ServerName www.railsapp2.com
ServerAlias railsapp2.com
...
For some reason, RailsApp1 doesn't require these changes to function correctly.
精彩评论