# Apache + Mongrel + Static files

**URL:** <https://www.ruby-forum.com/t/apache-mongrel-static-files/123545>\
**Category:** Deployment\
**Created:** [December 18, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545 "2007-12-18T16:37:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 18, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/1 "2007-12-18T16:37:56Z")

</div>

Hi!

I deployed my first rails application on my own server (I’m using Apache  
2.2.6 + Mongrel).

It’s working perfectly and I’m very happy.

The only one problem is that it is very slow to render the layout at the  
first time. I think that the problem comes from static files like images  
and css because the text is rendered directly but images appears one  
by one and very slowly.

So I configured my Apache virtual host like this :

\<Proxy balancer://myapp\_cluster\>  
BalancerMember [http://127.0.0.1:8000](http://127.0.0.1:8000)  
BalancerMember [http://127.0.0.1:8001](http://127.0.0.1:8001)  
BalancerMember [http://127.0.0.1:8002](http://127.0.0.1:8002)

\<VirtualHost \*:80\>

ServerName [myapp.com](http://myapp.com)

ErrorLog logs/myapp\_errors\_log  
CustomLog logs/myapp\_log combined

DocumentRoot /path\_to\_app/public

\<Directory “/path\_to\_app/public”\>  
Options FollowSymLinks  
AllowOverride None  
Order allow,deny  
Allow from all

RewriteEngine On

# Rewrite index to check for static

RewriteRule ^/$ /index.html [QSA]

# Rewrite to check for Rails cached page

RewriteRule ^([^.]+)$ $1.html [QSA]

# Redirect all non-static requests to cluster

RewriteCond %{DOCUMENT\_ROOT}/%{REQUEST\_FILENAME} !-f  
RewriteRule ^/(.\*)$ balancer://myapp\_cluster%{REQUEST\_URI} [P,QSA,L]

# Deflate

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml  
application/xml application/xhtml+xml text/javascript  
BrowserMatch ^Mozilla/4 gzip-only-text/html  
BrowserMatch ^Mozilla/4.0[678] no-gzip  
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

ProxyPass /images !  
ProxyPass /stylesheets !  
ProxyPass /javascripts !

Alias /images /path\_to\_app/public/images  
Alias /stylesheets /path\_to\_app/public/stylesheets  
Alias /javascripts /path\_to\_app/public/javascripts

And I restarted httpd but it doesn’t seem to change anything.

Am I doing something wrong?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 18, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/2 "2007-12-18T18:30:03Z")

</div>

Best way to debug behaviour is to turn on rewrite logging. You’ll see  
what rewrite rule is activated.

RewriteLog [your\_path\_here]/http\_rewrite\_log  
RewriteLogLevel 9

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 18, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/3 "2007-12-18T22:20:51Z")

</div>

Thanks for you reply.

I get this in my log file for each images, css and js files :

[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (2) init rewrite  
engine with requested uri /images/logo.png  
[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (3) applying pattern  
‘^/$’ to uri ‘/images/logo.png’  
[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (3) applying pattern  
‘^([^.]+)$’ to uri ‘/images/logo.png’  
[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (3) applying pattern  
‘^/(.\*)$’ to uri ‘/images/logo.png’  
[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (4) RewriteCond:  
input=’/var/www/rails/myapp/public//images/logo.png’ pattern=’!-f’ =\>  
not-matched  
[18/Dec/2007:22:03:51 +0100]  
[myapp/sid#800f6270][rid#80258cc0/initial] (1) pass through  
/images/logo.png

I presume that the problem come from the // in (4) RewriteCond:  
input=’/var/www/rails/myapp/public//images/logo.png’but I don’t  
understand why it appears.

Do you have any idea?

Thanks!

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 19, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/4 "2007-12-19T13:30:17Z")

</div>

Thanks, I don’t have the duplicated / anymore but I still have the same  
message in the log file :

1. RewriteCond:  
input=’/var/www/rails/myapp/public/images/logo.png’ pattern=’!-f’ =\>  
not-matched

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 20, 2007, 10:43am UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/5 "2007-12-20T10:43:22Z")

</div>

It could be a permissions issue. Can the user that apache is running as  
read the file?

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 19, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/6 "2007-12-19T12:43:41Z")

</div>

This is the one being executed. Your request file name has a leading ‘/’

Try removing the duplicate / from below

RewriteCond %{DOCUMENT\_ROOT}/%{REQUEST\_FILENAME} !-f

becomes

RewriteCond %{DOCUMENT\_ROOT}%{REQUEST\_FILENAME} !-f

It may have more success

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 21, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/7 "2007-12-21T16:38:46Z")

</div>

Well, I disabled iptables on my server and now images are displayed very  
fastly (but it doesn’t explain messages in my log file).

The problem now is why iptables slows down images like this? I need a  
protection for my server.

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [December 20, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/8 "2007-12-20T13:55:18Z")

</div>

Yes, these files are owned by mongrel user but all users are allowed to  
read them :

$ ls -al /var/www/rails/myapp/public/images/

-r-xr-xr-x 1 mongrel mongrel 2881 Nov 10 16:00 bg.png  
-r-xr-xr-x 1 mongrel mongrel 5582 Nov 10 16:01 footer.png  
…

$ ls -al /var/www

drwxr-xr-x 3 root root 4096 Dec 16 22:03 rails

$ ls -al /var/www/rails

dr-xr-xr-x 16 mongrel mongrel 4096 Dec 17 23:28 myapp

$ ls -al /var/ww/rails/myapp

dr-x------ 7 mongrel mongrel 4096 Dec 13 23:58 app  
dr-x------ 3 mongrel mongrel 4096 Dec 13 23:58 components  
dr-x------ 5 mongrel mongrel 4096 Dec 16 22:29 config  
drwx------ 4 mongrel mongrel 4096 Dec 13 23:58 db  
dr-x------ 3 mongrel mongrel 4096 Dec 13 23:58 doc  
dr-x------ 4 mongrel mongrel 4096 Dec 13 23:58 lib  
-r-x------ 1 mongrel mongrel 478 Dec 9 12:10 .loadpath  
drwx------ 3 mongrel mongrel 4096 Dec 16 22:47 log  
-r-x------ 1 mongrel mongrel 433 Dec 9 12:10 .project  
dr-xr-xr-x 6 mongrel mongrel 4096 Dec 17 23:28 public  
-r-x------ 1 mongrel mongrel 307 Oct 13 18:11 Rakefile  
dr-x------ 5 mongrel mongrel 4096 Dec 13 23:58 script  
dr-x------ 8 mongrel mongrel 4096 Dec 13 23:58 test  
drwx------ 7 mongrel mongrel 4096 Dec 13 23:58 tmp  
dr-x------ 5 mongrel mongrel 4096 Dec 16 21:46 vendor

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![2dc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/4bbf92/32.png) [@2dc](https://www.ruby-forum.com/u/2dc)\
**Post date:** [January 13, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/apache-mongrel-static-files/123545/9 "2008-01-13T12:42:55Z")

</div>

Anthony 2dc wrote:

> Well, I disabled iptables on my server and now images are displayed very  
> fastly (but it doesn’t explain messages in my log file).
> 
> The problem now is why iptables slows down images like this? I need a  
> protection for my server.

Are you sure you didn’t change something else as well? Iptables is fast.

Might be more useful to browse you site with Firefox with the Firebug  
extension installed. On the Firebug interface there is a “Net” tab, you  
can see the response headers for each file your browser requested, more  
specifically you can see which server (apache or mongrel) is serving  
those files.
