Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
New Rails app in Nginx+Passenger

New Rails app in Nginx with Passenger

This gist shows how to setup and configure Rails application in Nginx on Centos.

Our application will be located in '/var/www/apps/mysite'.


After installing Passenger Nginx will be located in '/opt/nginx' directory.

Read this to install Passenger and Nginx on Centos.

Server in Nginx

Create configuration file '/opt/nginx/sites-available/mysite.conf' for server (virtual host) in Nginx:

server {
  passenger_enabled on;
  passenger_ruby /home/uadmin/.rvm/wrappers/ruby-2.1.5/ruby;  

  server_name ;
  root /var/www/apps/mysite/current/public;
  rails_env production;

  access_log /var/www/logs/mysite/access.log;
  error_log /var/www/logs/mysite/error.log;

  if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html break;


It assumes that Rails app will be running with Ruby 2.1.5. If you have different version of Ruby change the line passenger_ruby /home/uadmin/.rvm/wrappers/ruby-2.1.5/ruby; to point to your Ruby binary. It is useful if you have multiple ruby versions. If you specify the same Ruby version in Nginx main config file then you don't need to have this line passenger_ruby ... Find Nginx main config file in this gist below.

Create necessary directories:

mkdir /var/www/logs

cd /var/www/logs
mkdir mysite

Make the server enabled in Nginx:

sudo ln -s /opt/nginx/sites-available/mysite.conf /opt/nginx/sites-enabled/mysite.conf

Restart Nginx:

sudo service nginx restart
user nginx dev;
worker_processes 1;
error_log /var/www/logs/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/;
events {
worker_connections 1024;
http {
passenger_root /home/myrvmuser/.rvm/gems/ruby-2.1.5/gems/passenger-5.0.13;
passenger_ruby /home/myrvmuser/.rvm/gems/ruby-2.1.5/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/www/logs/nginx/access.log main;
sendfile on;
client_max_body_size 8m;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml image/png image/gif image/jpg;
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
access_log /var/www/logs/nginx/main.access.log main;
location / {
try_files $uri $uri/ =404;
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
# redis admin
#location /phpredisadmin {
# auth_basic "Login";
# auth_basic_user_file /var/www/html/phpredisadmin/.htpasswd;
location ~ /\. { deny all; }
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# proxy the PHP scripts to Apache listening on
#location ~ \.php$ {
# proxy_pass;
# pass the PHP scripts to FastCGI server listening on
#location ~ \.php$ {
# root html;
# fastcgi_pass;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
# deny all;
include /opt/nginx/conf.d/*.conf;
include /opt/nginx/sites-enabled/*;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment