codedecoder

breaking into the unknown…

passenger.conf file location on nginx or apache

Leave a comment

Recently, we have observed a strange behaviour on the staging server(nginx + passenger) , we have created for testing. It is found that, when a set of 10 or 15 users try to access the home page one after other, the first user took relatively more time (50 seconds against the average of 10 second) . We get clue about the problem here on stackoverflow .

PROBLEM : Passenger spawning new Rails instance for some request causing high load time for the request  .

The problem is that, our site remain idle for long time, since we are using it only for testing. The default passenger configuration for passenger_pool_idle_time allow idle time of 300 seconds, after which it shut down itself. It restart rails instance again ,when new request come, so basically it loading all the configuration, database.yml, initialization etc, which results in more loading time for the user visiting first. Since, Rails instance got restarted the subsequent user visiting the site witness normal time of 10 seconds.

SOLUTION :  set passenger_pool_idle_time to 0

It will make passenger to never shut down it self even if the server is idle for long time. It cost you memory as inactive process never get killed.

So, if you are aware of traffic on your site, you can adjust accordingly. Say, one user visit your site every 10 minute. so you can set the value to 2×10 i,e 20 minutes.

passenger_pool_idle_time 1200 #the time should be in integre

You can get complete list of passenger configuration here .  Now the question is where we can write all these available options for passenger configuration. The Answer is that : All passenger configuration goes in nginx.conf or apache.conf file itself .

depending on the way, you have installed apache or nginx, there respective conf file may be present at below location:

/usr/local/nginx/conf/nginx.conf

or

/etc/nginx/conf/nginx.conf

or

/opt/nginx/conf/nginx.conf

path will remain same for apache.conf also . So we have hold on the respective conf file. Now you can add passenger related configuration in below two ways.

METHOD 1: Add the passenger configuration in nginx.conf file directly.

You can add passenger configuration in http , server or location block. I have added it to http block

worker_processes  4;
events {
    worker_connections  1024;
}

worker_rlimit_nofile    1200;

http {
      passenger_root /usr/local/rvm/gems/ruby-1.9.3-p125@new_customer_portal/gems/passenger-3.0.17;
      passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p125@new_customer_portal/ruby;
      passenger_pool_idle_time 0;

      include	  mime.types;
      default_type  application/octet-stream;
      underscores_in_headers on;

      sendfile        on;
      keepalive_timeout  65;

     server {
	          listen       80;        
            server_name  barcelona-portal.thirdpillar.com;
            root "/var/www/sunpower.com/current/public";
            passenger_enabled on;
            rails_env staging;
            error_page   500 502 503 504  /50x.html;

            location = /50x.html {
                   root   html;
             }

          }
     index  index.html index.htm;
}

The orange marked part is passenger configuration added to nginx.conf . But as your passenger configuration increase it become cumbersome to maintain. It is better to write all passenger configuration in passenger.conf file and include it in nginx.conf file. we will do it in next Method.

METHOD 2: Add passenger configuration in passenger.conf file and include it in nginx.conf file .

create passenger.conf file in the same folder where nginx.conf file present i,e say /etc/nginx/conf (you may create it anywhere, but it is a convention) folder and add passenger related configuration to it.

      passenger_root /usr/local/rvm/gems/ruby-1.9.3-p125@new_customer_portal/gems/passenger-3.0.17;
      passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p125@new_customer_portal/ruby;
      passenger_pool_idle_time 0;

Now include this file in nginx.conf file. So the configuration in Method 1, will now look like

worker_processes  4;
events {
    worker_connections  1024;
}

worker_rlimit_nofile    1200;

http {
      include /etc/nginx/conf/passenger.conf; # it load passenger.conf file

      include	  mime.types;
      default_type  application/octet-stream;
      underscores_in_headers on;

      sendfile        on;
      keepalive_timeout  65;

     server {
	          listen       80;        
            server_name  barcelona-portal.thirdpillar.com;
            root "/var/www/sunpower.com/current/public";
            passenger_enabled on;
            rails_env staging;
            error_page   500 502 503 504  /50x.html;

            location = /50x.html {
                   root   html;
             }

          }
     index  index.html index.htm;
}

REFERENCES:

https://www.phusionpassenger.com/documentation_and_support#documentation # passenger documentation

http://www.modrails.com/documentation/Users%20guide%20Nginx.html #passenger with nginx

http://www.modrails.com/documentation/Users%20guide%20Apache.html # passenger with apache

Advertisements

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s