Apache + Mongrel + Static files

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
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002

<VirtualHost *:80>

ServerName 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!

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

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!

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

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

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

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.

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs